[R] Is conditional evaluation of R code chunks possible in Sweave ?

Paul.Rustomji at csiro.au Paul.Rustomji at csiro.au
Thu Jul 31 02:29:09 CEST 2008


Hi R mailing list

Does anyone know of a good way to conditionally evaluate R code chunks
when Sweaveing?

What I'd kind of like to be able to do is set a number of variables in a
list and if variable.a=TRUE then process code chunk labelled "methodA",
if variable.b=TRUE then process code chunk labelled "methodB" etc

<<label=setvars>>=
variable.a <- TRUE
variable.b <- FALSE ##etc
@

if (varible.a == TRUE) {
<<label=methodA>>=
R code for method A goes in here
more R code
@
}

if (varible.b == TRUE) {
<<label=methodb>>=
R code for method B goes in here
more R code
@
}


but the problem under this is the if statement is evaluated *outside*
the R code chunk.  I know how to use Latex \ifthenelse commands but
these only get evaluated during the latex compilation which occurs after
the Sweaving...

I know Sweave has the eval=TRUE/FALSE option but I don't think this is
evaluated in R meaning that one couldn't use an \Sexpr{result =
TRUE/FALSE} to set the eval=T/F option for the code chunk.

I'd ultimately like to have a separate methodA script and methodB script
that were called/sourced/Sweaved as required.  Sweaving within a code
chunk seems to work a bit, but I've been having troubles getting graphs
produced with the filenames I need.

Any ideas - or is the only option to evaluation all code chunks but with
the if statement *inside* the code chunk?

Paul Rustomji
Rivers and Estuaries
CSIRO Land and Water
GPO Box 1666
Canberra ACT 2601

ph +61 2 6246 5810
mobile 0406 375 739



More information about the R-help mailing list