-----Original Message----- From: William Dunlap
Sent: Saturday, May 19, 2012 11:07 AM
To: Rolf Turner
Cc: r-help
Subject: Re: [R] Names of Greek letters stored as character strings;plotmath.

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-boun...@r-project.org [mailto:r-help-boun...@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
<ggrothendi...@gmail.com> wrote:
> On Sat, May 19, 2012 at 1:18 AM, Rolf Turner <rolf.tur...@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))


-- snip --


This discussion has been exceedingly helpful, sort of.

Every time I try to do a task involving this I read the documentation for bquote(), expression(), plotmath(), etc., over and over, and I still fail to get the big picture of how R parses things under the hood. Typically, I only succeed each time by frustrating trial and error. Can I ask how you guys got a handle on the bigger (besides your usual brilliance <G>)?

Is there more comprehensive documentation in the developer literature or is there a user wiki that you would recommend for those who never quite get the big picture? If not, this would be a worthy topic for an R Journal article if someone has knowledge and the time to do it. Wish I were knowledgeable enough to do it myself.

Thanks,

Rob
______________________________________________
R-help@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.


------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965
______________________________________________
R-help@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.

Reply via email to