[R] Help with expression()

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Oct 11 19:50:11 CEST 2013


On 10.10.2013 21:40, Sheri wrote:
> Hi everyone,
>
> I am hoping someone can help with my attempted use of the expression
> function. I have a long series of text and variable to paste together
> including a degree symbol. The text is to be placed on my scatter plot
> using the mtext function.
>
> Using expression like this:
>
> changetext = expression(paste("Change from ",mini," to ", maxi, ":",
> diff ,degree,"C",collapse=""))
>
> does not evaluate my user defined variables - mini,maxi, and diff -
> just printing them out as words
>
> Using expression like this:
>
> changetext = paste("Change from ",mini," to ", maxi, ":", diff
> ,expression(degree,"C"),collapse="")
>
> prints the text twice and does not evaluate the degree symbol.
>
> I have tried to place the expression alone in a variable and then run the paste:
>
> degsym = expression(degree,"C")
> changetext = paste("Change from ",mini," to ", maxi, ":", diff
> ,degsym,collapse="")
>
> giving me the same result as the second option
>
> Is there any way I can use the expression function as in the first
> example but still have R evaluate my user defined variables?
>
> Thanks!
> Sheri

I'd rather use substitute for substitution of the symbols and get:



plot(1, main = substitute("Change from " * mini * " to " * maxi * ": " * 
diff * degree * "C", list(mini=mini, maxi=maxi,diff=diff)))

Best,
Uwe Ligges



More information about the R-help mailing list