[R] Sweave question
Jason Turner
jasont at indigoindustrial.co.nz
Fri Dec 26 11:36:12 CET 2003
Rafael A. Irizarry wrote:
> Using Sweave in the tools library (R version 1.8.0: sorry i havent
> upgraded), it seems i cant use if statements in R chunks that make graphs.
> i have this:
>
> <<fig=TRUE,echo=F>>=
> par(mfrow=c(1,1))
> if(exists("x"))
> plot(x,x)
> else{
> plot(1,1,type="n")
> text(1,1,"data not available.\n")
> }
> @
>
> and I get this error:
>
> Error: chunk 6
> Error in parse(file, n, text, prompt) : parse error
>
> any help is appreciated.
>
Whenever you get an error message, copy and paste it into an R session
(interactive) and see what happens. At least then you'll know where the
problem is, and whether it's a Sweave problem or a problem with your R
syntax (in this case, it's the second option. sorry).
This problem is almost, but not quite, a FAQ.
The problem is that the "if(...)" is syntactically complete after the
first plot() command. The "else {" appears out of nowhere, as far as
the R interpreter is concerned, and this makes no sense.
The fixes:
1) Always do this:
if(something) {
do something
} else {
do something else
}
Note exactly (!) where the curly braces are, relative to the "else".
2) wrap the whole thing in curly braces.
{
if(something)
do somthing
else {
do something else
}
}
Cheers
Jason
--
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
jasont at indigoindustrial.co.nz
More information about the R-help
mailing list