Cannot use dput for data.table in R

前端 未结 3 2145
小蘑菇
小蘑菇 2020-12-03 17:57

I have following data.table for which I cannot use output of dput command to recreate it:

> ddt
   Unit Anything index new
1:    A      3.4     1   1
2:           


        
3条回答
  •  没有蜡笔的小新
    2020-12-03 18:36

    I have also found this behavior rather annoying. So I have created my own dput function that ignores the .internal.selfref attribute.

    dput <- function (x, file = "", control = c("keepNA", "keepInteger", 
                                        "showAttributes")) 
    {
      if (is.character(file)) 
        if (nzchar(file)) {
          file <- file(file, "wt")
          on.exit(close(file))
        }
      else file <- stdout()
      opts <- .deparseOpts(control)
      # adding these three lines for data.tables
      if (is.data.table(x)) {
        setattr(x, '.internal.selfref', NULL)
      }
      if (isS4(x)) {
        clx <- class(x)
        cat("new(\"", clx, "\"\n", file = file, sep = "")
        for (n in .slotNames(clx)) {
          cat("    ,", n, "= ", file = file)
          dput(slot(x, n), file = file, control = control)
        }
        cat(")\n", file = file)
        invisible()
      }
      else .Internal(dput(x, file, opts))
    }
    

提交回复
热议问题