Prevent print() from outputting list indices in R

走远了吗. 提交于 2019-12-23 10:05:23

问题


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:

[[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:

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

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


回答1:


You can cheat with 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)))
)


来源:https://stackoverflow.com/questions/3968693/prevent-print-from-outputting-list-indices-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!