[R] dput and dget error
dwinsemius at comcast.net
Fri Dec 23 23:24:23 CET 2016
> On Dec 23, 2016, at 12:45 PM, Carl Sutton via R-help <r-help at r-project.org> wrote:
> Merry Christmas and Happy Holidays
> I am attempting to use dput and or dget to send data along with a help request to another list (package specific). Read the help pages for both and they appeared to be fairly simple functions. Found an example on github and it appeared to be an easy task to replicate. Alas, a copy and paste of the github example worked but my toy data example did not.
> # dput and dget functions
> # Example from github
> NN <- 10
> theData <- data.frame(Alpha = rnorm(NN),
> Beta = rnorm(NN))
> theData$Gamma <- theData$Alpha * 2 + theData$Beta / 2 + rnorm(NN)
> dput(theData, "temporary_file")
> theDataReconstitutedAgain <- dget("temporary_file")
> # try it with my "toy data"library(data.table)
This is a guess based on what I know about data.table-objects and how they are represented with dput. You will see an .internal.selfref = <pointer: 0x7f7fd18f2778>
But if you try to assign the value of the dput output to an R name, it's going to fail. You need to remove the .internal.selfref = <pointer: 0x7f7fd18f2778> and then assign as a data frame and run setDT() on the name.
> # data table for melt and columns split
> dt1 <- data.table(a_1 = 1:10, b_2 = 20:29,folks = c("art","brian","ed",
> "rich","dennis","frank", "derrick","paul","fred","numnuts"),
> a_2 = 2:11, b_1 = 21:30)
> melted <- melt(dt1, id = "folks")[,c("varType","varIndex") :=
> # melted has 40 observations from stacking a and b variables
> # which have lengths of 20 each
> # here cometh the frustrtion
> goGetIt <- dget("temp_file.txt", keep.source = FALSE)
>> goGetIt <- dget("temp_file.txt", keep.source = FALSE)
> Error in parse(file = file, keep.source = keep.source) :
> temp_file.txt:18:36: unexpected '<'
That's because the regular R parser doesn't recognize the pointer as valid R code. It was not designed to recognize data.tables. the tidyr functions are designed to return data.frames, but `dget` is not from a package that is "data.table-aware"
> 17: "varIndex"), row.names = c(NA, -40L), class = c("data.table",
> 18: "data.frame"), .internal.selfref = <
> Error in print(goGetIt) : object 'goGetIt' not found
Try this instead:
goGetIt <- dget("temp_file.txt", keep.source = FALSE)
> the help page states dput just writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object. Nothing there about not allowing a data frame or data table and the github example was a data frame. The line numbers referenced do not appear to refer to my code (but maybe they do and I am ignorant of the meaning) and the code works up to the dget line.
> Typing dget in the console to see the inner workings of the function was not helpful.
> Also, I am unclear as to just how dput and dget work. If I save a subset of actual data to an object, then do dput on that object, can I rely that whomever I send my code to (which includes the dput statement) be able to retrieve the data? In other words, dput saves the data in the code file?
> Thanks for your help and may the holidays be wonderful for you and your loved one.
> Carl Sutton
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help