I had exactly the same question. I was wondering if something was built in for this, or if I would need to write it myself. I did not find anything built-in, so I wrote the following functions:
dputToString <- function (obj) { con <- textConnection(NULL,open="w") tryCatch({dput(obj,con); textConnectionValue(con)}, finally=close(con)) } dgetFromString <- function (str) { con <- textConnection(str,open="r") tryCatch(dget(con), finally=close(con)) }
I think this does what you want. Here is the test:
> rep <- dputToString(matrix(1:10,2,5)) > rep [1] "structure(1:10, .Dim = c(2L, 5L))" > mat <- dgetFromString(rep) > mat [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10
ralmond
source share