Does R have an equivalent of Python's "repr" (or Lisp's "prin1-to-string")?
Asked Answered
M

4

9

I occasionally find that it would be useful to get the printed representation of an R object as a character string, like Python's repr function or Lisp's prin1-to-string. Does such a function exist in R? I don't need it to work on complicated or weird objects, just simple vectors and lists.

Edit: I want the string that I would have to type into the console to generate an identical object, not the output of print(object).

Moonshiner answered 2/4, 2012 at 19:53 Comment(3)
Maybe you're looking for dput?Eckart
Most objects in R have a default print method that does what you're asking for if you just call them on the command line. > x <- 1:10 > xAerophagia
@Justin, I mean I want the representation that I would type into the R console to construct the object, just like repr does in Python (for supported objects).Moonshiner
E
17

I'm not familiar with the Python/Lisp functions you listed, but I think you want either dput or dump.

x <- data.frame(1:10)
dput(x)
dump("x", file="clipboard")
Eckart answered 2/4, 2012 at 21:12 Comment(2)
Yes, dump/dput look like they do what I want. Thank you.Moonshiner
This will work for simple R objects (vectors, matrices, data frames, lists of those things, S4 classes), but fails for environments and anything with an environment in it (closures, lists with environment elements) maybe other more complex classes too.Phasia
S
6

See ?evaluate in the evaluate package.

EDIT: Poster later clarified in comments that he wanted commands that would reconstruct the object rather than a string that held the print(object) output. In that case evaluate is not what is wanted but dput (as already mentioned by Joshua Ullrich in comments and since I posted has been transferred to an answer) and dump will work. recordPlot and replayPlot will store and replot classic graphics on at least Windows. trellis.last.object will retrieve the last lattice graphics object. Also note that .Last.value holds the very last value at the interactive console.

Scaife answered 2/4, 2012 at 20:32 Comment(0)
R
2

You can use capture.output:

repr <- function(x) {
  paste(sprintf('%s\n', capture.output(show(x))), collapse='')
}

For a version without the line numbers something along these lines should work:

repr <- function(x) {
  cat(sprintf('%s\n', capture.output(show(x))), collapse='')
}
Reposeful answered 2/4, 2012 at 20:15 Comment(2)
Just use cat in stead of implicitely printing the result. That should get rid of the line numbers.Underlet
I don't want the output of print(object), I want the string of characters that I would type in to create the object.Moonshiner
H
2

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 didn't 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 a 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
Homeopathy answered 3/3, 2015 at 1:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.