"翻译可读"打印(在R中)

Elv*_*vis 4 printing r

我想知道是否有一种方法以"解释器可读"的方式打印对象,这将做类似这样的事情:

> x <- c(1:5,8)
> print.ir(x)
 c(1,2,3,4,5,8)
> x <- matrix(1:4, ncol=2)
> print.ir(x)
 matrix(c(1,2,3,4), ncol=2, nrow=2)
Run Code Online (Sandbox Code Playgroud)

这样结果可以在R脚本或另一个R会话中进行复制粘贴.

And*_*rie 5

使用dput()此:

x <- c(1:5,8)
dput(x)
c(1, 2, 3, 4, 5, 8)

x <- matrix(1:4, ncol=2)
dput(x)
structure(1:4, .Dim = c(2L, 2L))
Run Code Online (Sandbox Code Playgroud)

试试吧:

z <- structure(1:4, .Dim = c(2L, 2L))
z
     [,1] [,2]
[1,]    1    3
[2,]    2    4
Run Code Online (Sandbox Code Playgroud)