Suppose an object is already defined in the workspace:
a <- round( rnorm(10) )
[1] 0 -1 -1 -1 -1 0 2 1 1 1
How can I programatically generate a command which creates a
?
For example, I would like to use the a
in my workspace to generate the following string codeToCreateA
:
codeToCreateA <- "a <- c( 0, -1, -1, -1, -1, 0, 2, 1, 1, 1)"
I'm interested in the general case, in which a
could be any class of object, including a vector, list, or data frame.
dput(a)
? Not sure what you mean by "as a string"numeric
vector ?dput(a)
works in this case. Thanks! I just tried it on a data frame and got this resultstructure(list(A = c("a", "a", "a", "b", "b"), B = 1:5), .Names = c("A", "B"), row.names = c(NA, -5L), class = c("data.table", "data.frame" ), .internal.selfref = <pointer: 0x22f5938>)
. How can I create the data frame again from this code?dput(a)
to whatever variable name you want.dput(a, file="a.R")
, then usedget()
to get it. Something likerm(a); assign("a", dget("a.R")); a
would do it.