[R] dput and dget error

David Winsemius 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
> set.seed(1337)
> 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")
> print(theDataReconstitutedAgain)
> 
> 
> #  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.


> library(tidyr)
> #  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") :=
>                                 tstrsplit(variable,"_")][,variable:=NULL]

Run this:

dput(melted)


> #  melted has 40 observations from stacking a and b variables
> #  which have lengths of 20 each
> #  here cometh the frustrtion
> dput(melted,"temp_file.txt")
> goGetIt <- dget("temp_file.txt", keep.source = FALSE)
> print(goGetIt)
> 
> 
>> 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 = <
> ^
>> print(goGetIt)
> Error in print(goGetIt) : object 'goGetIt' not found
>> 

Try this instead:

 dput(as.data.frame(melted),"temp_file.txt")
 goGetIt <- dget("temp_file.txt", keep.source = FALSE)
 setDT(goGetIt)
 print(goGetIt)

-- 
David.
> 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
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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 mailing list