Search code examples
stringrrepresentation

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


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).


Solution

  • 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")