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

From: <Paul.Rustomji_at_csiro.au>
Date: Thu, 31 Jul 2008 10:29:09 +1000


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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Jul 2008 - 00:38:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Jul 2008 - 01:33:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive