r - 防止 print() 在 R 中输出列表索引

标签 r list loops printing

我有一个 list包含六个图,如下所示:

voi=c('inadist','smldist','lardist')

plist <- llply(voi, 
    function(v,df,s) {
        list(   
            assign(
                paste(v,'.violin'), 
                bwplot(groupname~df[,which(colnames(df)==v)]|fCycle*fPhase, 
                    data=df, 
                    groups=groupname, col=rainbow(1), box.ratio=3,
                    main=paste('Distribution of ', v, ' by Treatment and Cycle'),
                    sub=s, xlab=v, panel=panel.violin)),
            assign(
                paste(v,'.hexbin'),
                hexbinplot(df[,which(colnames(df)==v)]~starttime|groupname, 
                    data=df, xlab='Time(s)',main= paste('Distribution of ',v,' by Treatment'),
                    sub=s,ylab=v, aspect=0.5, colramp=redgrad.pal, layout=c(2,4)))

            )
    },data,meta$exp_name)

如果我打印列表,print(plist) ,绘图输出到图形设备,然后索引输出到控制台,结果如下:
[[1]]
[[1]][[1]]

[[1]][[2]]


[[2]]
[[2]][[1]]

[[2]][[2]]


[[3]]
[[3]][[1]]

[[3]][[2]]

因为我正在编写一个 web 应用程序,所以我需要非常严格地控制控制台输出。到目前为止,我可以在不输出索引的情况下输出图的唯一方法是这样的:
for(p in plist) 
    for(i in p) 
        print(i)

有没有更有效的方式来获得我需要的东西?

最佳答案

你可以用 capture.output 作弊:

dummy <- capture.output(print(plist))

或不创建新变量
invisible(capture.output(print(plist)))

顺便说一下,可重现的示例如下所示:
require(lattice)
plist <- list(
    list(bwplot(rnorm(10)),bwplot(rnorm(10))),
    list(bwplot(rnorm(10)),bwplot(rnorm(10))),
    list(bwplot(rnorm(10)),bwplot(rnorm(10)))
)

关于r - 防止 print() 在 R 中输出列表索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3968693/

相关文章:

r - R 栅格包中的提取和重采样函数 : Area-Weighted Values

python - python 中的列表排列

r - 将循环输出存储在 R 中的数据帧中

r - 通过 R 中的组查找 boolean 值是否为真

list - F# 将值添加到现有列表

python - 在 python 中使用列表时遇到问题

python - 如何在重复数字的python中创建矩阵?

java - 保持循环直到正确的输入

javascript - 添加到 JS 脚本中时,iMacros 脚本无法正确循环

r - 用户定义的函数在 dplyr 管道中不起作用