[R] Names of Greek letters stored as character strings; plotmath.

William Dunlap wdunlap at tibco.com
Sat May 19 18:07:41 CEST 2012


parse(text=paste(...)) works in simple cases but not in others.  The
fortune about it is there because it is tempting to use but if you bury it
in a general purpose function it will cause problems when people
start using nonstandard names for variables.  bquote(), substitute(),
call(), and relatives work in all cases.  E.g.,

  > par(mfrow=c(2,1))
  > power <- "gamma" ; x <- "Waist" ; y <- "Weight" # valid R variable names
  > plot(0, main=bquote(.(as.name(x))^.(as.name(power))/.(as.name(y))))
  > plot(0, main=parse(text=paste0(x, "^", power, "/", y))) # same as previous
  >
  > power <- "gamma" ; x <- "Waist Size (cm)" ; y <- "Weight (kg)" # invalid R names
  > plot(0, main=bquote(.(as.name(x))^.(as.name(power))/.(as.name(y))))
  > plot(0, main=parse(text=paste0(x, "^", power, "/", y))) # whoops
  Error in parse(text = paste0(x, "^", power, "/", y)) :
    <text>:1:7: unexpected symbol
  1: Waist Size
           ^

Now you might say that serves me right for using weird variable names,
but some of us use R as a back end to a GUI system (one not designed
around R) and don't want to inflict on users R's rules for names when
we do not have to.

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 Bert Gunter
> Sent: Saturday, May 19, 2012 7:24 AM
> To: Gabor Grothendieck
> Cc: r-help
> Subject: Re: [R] Names of Greek letters stored as character strings; plotmath.
> 
> ... and here is another incantation that may be  informative.
> 
> xnm<- as.name("gamma')  ## This does the parsing
> plot(0, xlab =bquote(.(xnm))
> 
> The initial puzzle is that if you just set
> xnm <- "gamma"
> 
> bquote will insert the string "gamma" rather than the symbol. After
> all, that's what plotmath sees for xnm. So the key is telling plotmath
> that it's a symbol, not a string. This can either be done before, as
> above, or inline, as you and Gabor showed. Unsurprisingly. this also
> does it, since as.name() is doing the parsing:
> 
> xnm <- "gamma"
>  plot(0,xlab=bquote(.(as.name(xnm))))
> 
> AND we are adhering to Thomas's dictum: bquote is a wrapper for
> substitute(), which is what he recommends as the preferable
> alternative to eval(parse(...)) . But, heck -- all such software
> principles are just guidelines. Whatever works (robustly).
> 
> HTH.
> 
> Cheers,
> Bert
> 
> On Sat, May 19, 2012 at 3:17 AM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
> > On Sat, May 19, 2012 at 1:18 AM, Rolf Turner <rolf.turner at xtra.co.nz> wrote:
> >>
> >> I had such good luck with my previous question to r-help, (a few minutes
> >> ago) that I thought I would try again with the following query:
> >>
> >>> Suppose I have
> >>>
> >>>    xNm <- "gamma"
> >>>
> >>> I would like to be able to do
> >>>
> >>>    plot(1:10,xlab = <something involving xNm">)
> >>>
> >>> and get the x axis label to be the Greek letter gamma
> >>> (rather than the literal text string "gamma").
> >>>
> >>> Is this possible?  I've messed around with substitute()
> >>> and bquote() and got nowhere.
> >>
> >>
> >> Then, just before clicking on "Send", I had one more thimk, and blow
> >> me down, I got something that worked:
> >>
> >> plot(1:10,xlab=eval(expression(parse(text=xNm))))
> >>
> >
> > That can be shortened to:
> >
> > plot(0, xlab = parse(text = xNm))
> >
> > --
> > Statistics & Software Consulting
> > GKX Group, GKX Associates Inc.
> > tel: 1-877-GKX-GROUP
> > email: ggrothendieck at gmail.com
> >
> > ______________________________________________
> > 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.
> 
> 
> 
> --
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-
> biostatistics/pdb-ncb-home.htm
> 
> ______________________________________________
> 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