tags:

views:

66

answers:

2

I have a list containing 6 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?

+5  A: 

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)))
)
Marek
A reproducible example would set a seed, first ;)
Greg
@Greg touché. I should use `1:10` ;)
Marek
+1 -- good trick for the answer, and nice hint for the reproducible example.
Dirk Eddelbuettel
dnagirl
A: 

Another hack :

library(plyr)
library(lattice)

plist <- list(
    bwplot(voice.part ~ height, data=singer, xlab="Height (inches)"),
    dotplot(variety ~ yield | year * site, data=barley)
)

x <- lapply(plist,print)
rm(x)
Joris Meys
@Joris Meys: what is tt?
dnagirl
@dnagirl: sorry, copy-paste error. I corrected it now.
Joris Meys
@Joris Meys: on my webapp, R is called from PHP through curl() to a local RApache installation. The R script makes a data file and a pdf file and returns a JSON string containing the filenames. The current problem happens when I'm writing to the pdf file. The printed indices are included in the text returned to the php script. This screws things up significantly.
dnagirl
@Joris Meys: unfortunately, your solution only suppresses some output with my `plist`. I think it's because your example uses a simple list, while my plist is a list with 3 items, each of which is a list of 2 items.
dnagirl
@dnagirl: I realized. also the textConnection is not really an option, as that surpresses the call to show() for the individual list elements. This is a darn tricky one...
Joris Meys