Cannot use dput for data.table in R
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))
}
The problem is that dput
prints out external pointer address (this is something that data.table
uses internally, and will reconstruct when required), which you can't really use.
If you manually cut out the .internal.selfref
part, it will work just fine, except for a one-time complaint from data.table
for some operations.
You could add an FR to data.table
about this, but it will require modifying the base function from data.table
, similar to how rbind
is currently handled.