Extract command from data.frame to create the same df

is there any package / command in R that reads data.frame and then builds a command that can be used to create exactly the same data file without loading the data, i.e. all data.frame data would have to be stored in the command?

eg. if you have data.frame like this:

mydata <- data.frame(col1=c(1,2),col2=c(3,4))

      

I just want to get the command, so reading "mydata" results in the command on the right side.

BR Fabian

+3


source to share


2 answers


Function dput

"Writes the ASCII text representation of the R object to a file or connection" and as close to the right side as possible. In fact, it contains more details about the structure of the object, as shown below:



> dput(mydata)
structure(list(col1 = c(1, 2), col2 = c(3, 4)), .Names = c("col1", 
"col2"), row.names = c(NA, -2L), class = "data.frame")

      

+2


source


You can also use enquote

that converts mydata

back to invaluable call

. It can then be assessed with eval

.



> ( e <- enquote(mydata) )
# quote(list(col1 = c(1, 2), col2 = c(3, 4)))
> eval(e)
#   col1 col2
# 1    1    3
# 2    2    4
> identical(eval(e), mydata)
# [1] TRUE

      

+1


source







All Articles