阻止print()输出R中的列表索引

时间:2010-10-19 13:13:27

标签: 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]]

因为我正在编写一个webapp,我需要非常严格地控制控制台输出。到目前为止,我可以在不输出索引的情况下输出图的唯一方法是:

for(p in plist) 
    for(i in p) 
        print(i)

是否有更有效的方法来获得我需要的东西?

1 个答案:

答案 0 :(得分:5)

您可以使用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)))
)