Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent print() from outputting list indices in 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)

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?

like image 558
dnagirl Avatar asked Oct 19 '10 13:10

dnagirl


1 Answers

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)))
)
like image 67
Marek Avatar answered Sep 21 '22 04:09

Marek