2

对于某些包,我可以看到特殊类型的对象。例如,当我尝试从包中打印数据集时收到以下消息。

多性状

This is an object of class "cross".
  It is too complex to print, so we provide just this summary.
    RI strains via selfing

    No. individuals:    162

......................and other summary information

是(多重性状)

 [1] "riself"

我想知道我们如何创建这样的对象。它们是数据框的特殊列表,向量矩阵吗?

X <- c("A", "B", "C")
Y <- data.frame (A = 1:10, B = 21:30, C = 31:40)
myeq <- c("Y ~ X1 + Y1")
K <- 100
A = 1:20
B = B= 21:40
J <- as.matrix(A,B )
myl1 <- list(J, K)

现在我的复杂对象:

mycomplexobject <- list(X, Y, myeq, K, J, myl1) 
mycomplexobject
str(mycomplexobject)

List of 6
 $ : chr [1:3] "A" "B" "C"
 $ :'data.frame':       10 obs. of  3 variables:
  ..$ A: int [1:10] 1 2 3 4 5 6 7 8 9 10
  ..$ B: int [1:10] 21 22 23 24 25 26 27 28 29 30
  ..$ C: int [1:10] 31 32 33 34 35 36 37 38 39 40
 $ : chr "Y ~ X1 + Y1"
 $ : num 100
 $ : int [1:20, 1] 1 2 3 4 5 6 7 8 9 10 ...
 $ :List of 2
  ..$ : int [1:20, 1] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : num 100

是(我的复杂对象)

 [1] "list"   "vector"

有没有办法制作特殊对象并防止打印整个列表而不是像“打印很复杂”这样的消息并提供摘要?

4

1 回答 1

6

只需设置class您的对象并提供一个print方法。

class(mycomplexobject) <- c("too_complex", class(mycomplexobject))
print.too_complex <- function(x) {
  cat("Complex object of length", length(x), "\n")
}
mycomplexobject
于 2012-04-06T02:13:49.317 回答