[R] Sweave: Conditional code chunks?

Ralf B ralf.bierig at gmail.com
Mon Nov 15 18:49:00 CET 2010


I have a code junk that produces a figure. In my special case,
however, data does not always exist. In cases where data exists, the
code chunk is of course trival (case #1), however, what do I do for
case # 2 where the data does not exist?
I can obviously prevent the code from being executed by checking the
existence of the object x, but on the Sweave level I have a static
figure chunk. Here an example that should be reproducible:

# case 1
x <- c(1,2,3)
# case 2 - no definition of variable
#x <- c(1,2,3)

<<echo=FALSE, results=hide, fig=TRUE>>=
if(exists(as.character(substitute(meta.summary)))){
  plot(x)
}
@

In a way I would need a conditional chunk or a chunk that draws a
figure only if it was generated and ignores it otherwise.

Any ideas?

Ralf



More information about the R-help mailing list