Re: [R] Sweave question

About this list Date view Thread view Subject view Author view Attachment view

From: Jason Turner (jasont@indigoindustrial.co.nz)
Date: Fri 26 Dec 2003 - 21:36:12 EST


Message-id: <3FEC0F1C.2050709@indigoindustrial.co.nz>

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@indigoindustrial.co.nz

______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Thu 01 Jan 2004 - 09:21:04 EST