防止print()在R中输出列表索引 [英] Prevent print() from outputting list indices in R

查看:57
本文介绍了防止print()在R中输出列表索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样制作的情节:

I have a list containing six plots, made like this:

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),则将图输出到图形设备,然后将索引输出到控制台,结果是:

If I print the list, print(plist), the plots are output to the graphical device, then the indices are output to the console resulting in this:

[[1]]
[[1]][[1]]

[[1]][[2]]


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

[[2]][[2]]


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

[[3]][[2]]

因为我正在编写Web应用程序,所以我需要非常严格地控制控制台输出.到目前为止,我可以不输出索引而输出图的唯一方法是这样的:

Because I am coding a webapp, I need to control console output quite strictly. So far the only way I can output the plots without outputting the indices is like this:

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

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

Is there a more efficient way of getting what I need?

推荐答案

您可以使用capture.output作弊:

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

或不创建新变量

invisible(capture.output(print(plist)))


顺便说一句,可重现的示例如下:


By the way, reproducible example look like this:

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

这篇关于防止print()在R中输出列表索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆