Mark Clements
Mark Clements

Reputation: 465

Using "if else" (and possibly other) conditionals inside \Sexpr{}

Is it possible to use conditionals inside \Sexper{} in Sweave? An example of what I'm trying to do is

\Sexpr{if(coef(model1)[3]<0){-1*round(coef(model1)[3],3)}else{round(coef(model1)[3],3)}}

More elaborately, I want something like

\Sexpr{if(x<0){paste(-1*x, "lower", sep="")}else{paste(x, "higher", sep=""}}

When I try the first bit of code I get the following error:

Error in parse(text = cmd) : <text>:2:0: unexpected end of input
1:if(coef(model1)[3]<0){-1*round(coef(model1)[3],3)

Any ideas?

Thanks for your help,

-Mark

Upvotes: 1

Views: 1041

Answers (1)

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37754

Curly brackets are not allowed in Sexpr expressions. Instead do the computation in a hidden code chunk and use the result in an Sexpr.

See the Sweave manual: https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf

Upvotes: 4

Related Questions