Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我们在R中输入一个列表变量时,有没有办法决定打印什么?_R - Fatal编程技术网

当我们在R中输入一个列表变量时,有没有办法决定打印什么?

当我们在R中输入一个列表变量时,有没有办法决定打印什么?,r,R,我们定义了一个返回列表的函数。我们将返回的列表分配给一个变量。当我们将其输入控制台时,它将打印整个列表。是否有人喜欢从列表中打印什么 例如,当我输入“tm”时,我希望它只打印“tm$note”。。而不是打印填写控制台的整个列表:/ > summary(tm) Length Class Mode aucM 6 -none- numeric prbM 6 -none- numeric mapM 6 -n

我们定义了一个返回列表的函数。我们将返回的列表分配给一个变量。当我们将其输入控制台时,它将打印整个列表。是否有人喜欢从列表中打印什么

例如,当我输入“tm”时,我希望它只打印“tm$note”。。而不是打印填写控制台的整个列表:/

> summary(tm)
         Length Class  Mode     
aucM      6     -none- numeric  
prbM      6     -none- numeric  
mapM      6     -none- numeric  
perfAll  18     -none- numeric  
msdM     18     -none- numeric  
predlab   2     -none- list     
note      1     -none- character
settings  1     -none- character
> tm$note
[1] "this is an example of...bla bla.. "

设置列表的类别并定义一个
摘要
方法:

tm <- list(note="this is the note", junk="other junk", numericJunk=1:5)
class(tm) <- "myClass"
summary.myClass <- function(object, ...) {
     object$note
}
summary(tm)
## [1] "this is the note"
(您也可以使用
cat()
而不是
print()


如果需要,您可以将上述内容定义为
summary.list
print.list
,这样所有列表都将以这种方式打印,但这对我来说似乎很危险/过于笼统。。。最好的做法是指定你的值是一种特殊的列表(即给它分配一个类)。

如果不是你想要的,请随意编辑你的问题,让它更清楚……我不知道如何才能让它更清楚。。我不想在输入summary(tm)时得到那个结果,我只想在输入tm时得到它。。无论如何..@BenBolker-我喜欢你的答案,但我想我知道这里有什么问题。当您返回拟合的
lm
模型的名称时,您只会看到公式和系数。如何在不打印其中包含的所有信息的情况下完成此操作?好的,我想您需要定义
print
方法。见编辑后的答案。
 print.myClass <- function(object,...) {
     print(object$note)
 }
 tm
 ## [1] "this is the note"