[R] RE : Sweave: Conditional code chunks?

Wolfgang RAFFELSBERGER wraff at igbmc.fr
Mon Nov 15 19:12:23 CET 2010

Hi Ralf,

I first create (or not) the figure as a separate file(s) and then use conditional LaTeX to display the existing file(s) (exactely where I want it/them to appear).  This also works with png etc, but you'll have to specify the extensions. Just be careful if you change paths ...
This will give something like:

<<label=condFig1,fig=T,include=F>>=
wantToPlot <- TRUE
if(wantToPlot) {
pdf(file="fig1.pdf")
plot(1:10)     # watever you want to plot ...
}
@

bla .. bla .. bla .. bla ..

\ifnum \Sexpr{as.numeric(exists(fig1.pdf))}>0 {
\begin{figure}[!h]
\includegraphics {fig1}         %% fig1
\label{..}
\end{figure}
} \fi

Wolfgang

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wolfgang Raffelsberger, PhD
Laboratoire de BioInformatique et Génomique Intégratives
IGBMC,
1 rue Laurent Fries,  67404 Illkirch  Strasbourg,  France
Tel (+33) 388 65 3300         Fax (+33) 388 65 3276
wolfgang.raffelsberger  (at)  igbmc.fr

________________________________________
De : r-help-bounces at r-project.org [r-help-bounces at r-project.org] de la part de Ralf B [ralf.bierig at gmail.com]
Date d'envoi : lundi 15 novembre 2010 18:49
À : r-help Mailing List
Objet : [R] Sweave: Conditional code chunks?

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

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help