[R] Help with expression()

William Dunlap wdunlap at tibco.com
Fri Oct 11 00:19:59 CEST 2013


> 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

bquote() can do it: put the variables which should be evaluated in .().  E.g.,
    mini <- 13
    maxi <- 97
    diff <- maxi - mini
    plot(0, 0, main=bquote("Change from" ~ .(mini) ~ "to" ~ .(maxi) * ":" ~ .(diff) ~ degree ~ "C"))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Sheri O'Connor
> Sent: Thursday, October 10, 2013 1:08 PM
> To: R-help at r-project.org
> Subject: [R] Help with expression()
> 
> 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
> 
> ---
> Sheri O'Connor
> M.Sc Candidate
> Department of Biology
> Lakehead University  - Thunder Bay/Orillia
> 500 University Avenue
> Orillia, ON L3V 0B9
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list