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

时间:2023-01-24 21:43:40

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)

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:

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

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

[[1]][[2]]


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

[[2]][[2]]


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

[[3]][[2]]

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:

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

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

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

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

1 个解决方案

#1


5  

You can cheat with capture.output:

你可以使用capture.output作弊:

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

or without creating a new variable

或者不创建新变量

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

#1


5  

You can cheat with capture.output:

你可以使用capture.output作弊:

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

or without creating a new variable

或者不创建新变量

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