[R] dput and dget error
Carl Sutton
suttoncarl at ymail.com
Fri Dec 23 21:45:50 CET 2016
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)
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]
# 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 '<'
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
>
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
More information about the R-help
mailing list