如何自动将数据框控制台输出转换为其结构

How to transform a dataframe console output to its structure automatically

没有经验的 Whosebug 用户倾向于提供他们使用的数据的控制台输出:

  id name
1  1 john
2  2 mary

有经验的用户应该鼓励他们提供一个Reproducible Example,或者使用dput提供数据结构,但他们并不总是有时间。
您知道 package/function 允许 复制数据帧 的控制台输出并 从剪贴板生成相关结构 吗?

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

简而言之,我正在寻找并倒转dput
reprex 包允许这种操作,例如 clean-up console output,但我没有在其中找到我要找的功能!

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

使用 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"))