2

Unexperienced users of StackOverflow tend to provide console output of the data they use:

  id name
1  1 john
2  2 mary

Users with more experience should encourage them to provide a Reproducible Example, or to use dput to provide data structure, but they don't always have time.
Do you know of a package/function allowing to copy the console output of a dataframe and to generate out of the clipboard the associated structure?

structure(list(id = c(1, 2), name = c("john", "mary")), class = "data.frame", row.names = c(NA, 
-2L))

To make it short, I'm looking for and inverted dput.
The reprex package allows this kind of manipulation, for example clean-up console output, but I didn't find in it the function I'm looking for!

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • 1
    Related: [Code to import data from a Stack overflow query into R](https://stackoverflow.com/questions/10849270/code-to-import-data-from-a-stack-overflow-query-into-r). Just wrap answers in `dput`, I think. You should also read the FAQ [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), where you find `dput(read.table("clipboard",sep="",header=TRUE))` – Henrik Jun 15 '20 at 11:24
  • 1
    Doesn't really answer you question I guess, but you can also take a look at the `datapasta` package: https://github.com/milesmcbain/datapasta – markus Jun 15 '20 at 11:28

2 Answers2

1

Using read.table.

x <- "
  id name
1  1 john
2  2 mary
"

fun <- function(x, header=TRUE) dput(read.table(header=header, text=x))
fun(x)
# structure(list(id = 1:2, name = c("john", "mary")), class = "data.frame", row.names = c("1",
# "2"))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0
myVar <- structure(list(id = c(1, 2), name = c("john", "mary")), class = "data.frame", row.names = c(NA, 
-2L))
Limey
  • 10,234
  • 2
  • 12
  • 32