“翻译可读”打印(在R中)

时间:2013-01-10 15:01:56

标签: r printing

我想知道是否有一种以“解释器可读”的方式打印对象的方法,这样做会像这样:

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

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

1 个答案:

答案 0 :(得分: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))

试一试:

z <- structure(1:4, .Dim = c(2L, 2L))
z
     [,1] [,2]
[1,]    1    3
[2,]    2    4