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. thanks and apologies if this not a problem in R 1.8.1 rafael
Write the if-else statement as if(exists("x")) { plot(x,x) } else{ plot(1,1,type="n") text(1,1,"data not available.\n") } so the first two lines are not syntactically complete. "Rafael A. Irizarry" <ririzarr at jhsph.edu> writes:> 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. > > thanks and apologies if this not a problem in R 1.8.1 > rafael > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Douglas Bates bates at stat.wisc.edu Statistics Department 608/262-2598 University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/
This is the standard problem with if ... else: that will not work at the command line either. Use braces like if(exists("x")) { plot(x,x) } else { plot(1,1,type="n") text(1,1,"data not available.\n") } which should do the trick. On Tue, 23 Dec 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. > > thanks and apologies if this not a problem in R 1.8.1 > rafael > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Tue, 23 Dec 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") > } > @ >I think your problem is with the `else'. The `if' part of the statement is syntactically complete, so the parser isn't expecting an `else'. You need something like if (exists("x")){ plot(x,x) } else { plot(1,1,type="n") } -thomas
I'm not certain, but since you are getting a parse error, I would try curly brackets on your if statement: <<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") } @ HTH, + seth
On Tuesday 23 December 2003 09:44, 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)This is a problem even outside of Sweave. The last line above syntactically completes the if statement. You need to replace it by something like if(exists("x")) { plot(x,x) } else { ...> 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. > > thanks and apologies if this not a problem in R 1.8.1 > rafael > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Martin Maechler
2003-Dec-23 17:22 UTC
[R] Re: if .. else parse error {was "Sweave question"}
>>>>> "Rafael" == Rafael A Irizarry <ririzarr at jhsph.edu> >>>>> on Tue, 23 Dec 2003 10:44:38 -0500 (EST) writes:Rafael> Using Sweave in the tools library (R version 1.8.0: Rafael> sorry i havent upgraded), it seems i cant use if Rafael> statements in R chunks that make graphs. i have Rafael> this: Rafael> <<fig=TRUE,echo=F>> Rafael> par(mfrow=c(1,1)) Rafael> if(exists("x")) Rafael> plot(x,x) Rafael> else{ Rafael> plot(1,1,type="n") Rafael> text(1,1,"data not available.\n") Rafael> } Rafael> @ Rafael> and I get this error: Rafael> Error: chunk 6 Rafael> Error in parse(file, n, text, prompt) : parse error This has nothing to do with Sweave; it's a famous "beginner"s (;-) mistake: ?if has the explanation : if> Note that it is a common mistake to forget putting braces ('{ ..}') if> around your statements, e.g., after 'if(..)' or 'for(....)'. if> In particular, you should not have a newline between '}' and if> 'else' to avoid a syntax error in entering a 'if ... else' if> construct at the keyboard or via 'source'. For that reason, one if> (somewhat extreme) attitude of defensive programming uses braces if> always, e.g., for 'if' clauses. Regards, and Merry Christmas to all R-help readers! Martin
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