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

Joshua Wiley jwiley.psych at gmail.com
Mon May 21 04:52:56 CEST 2012


This may be a profoundly bad idea, but if deduction is beyond the skill of those with time, perhaps an inferential approach could be used.  Create examples of when one approach versus another works.  A book of spells, if you will, so that rather than hoping and typing wildly, the magic incantation can simply be looked up.

Josh

On May 20, 2012, at 19:28, Bert Gunter <gunter.berton at gene.com> wrote:

> Well, that's not very comforting, Duncan. It's like saying that you
> have to read the engineering specs to drive the car successfully.
> 
> I am certainly ignorant of the internals, but I honestly believe
> well-written documentation on these points would help. I learned a lot
> when I read V&R's "S Programming" some years ago, so I think that's an
> existence theorem.
> 
> Unfortunately, writing such documentation is hard, maybe harder than
> writing the code even (my own experience documenting my own code). And
> your logical response -- "Bert, R is open source and would welcome
> your efforts" -- is not going to get much traction. Like you, I don't
> have the time (nor, in my case, the smarts).
> 
> Nevertheless, I agree with Robert's sentiment that it could be done.
> In particular, plotmath docs really should expand their explanation of
> appropriate syntax, imho.
> 
> As always, I end by acknowledging the extraordinary efforts of R's
> developers. Your success is what has led to such carping. A response
> of "Aw shaddup" would therefore not be out of line.
> 
> Best,
> Bert
> 
> On Sun, May 20, 2012 at 5:00 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> wrote:
>> On 12-05-20 6:53 PM, Robert Baer wrote:
>>> 
>>> -----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-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))
>>> 
>>> 
>>> 
>>> -- 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.
>> 
>> 
>> I think you have to try writing C code to work with R objects to really
>> understand what's going on.  Reading those sections of the Writing R
>> Extensions manual will probably help, but actually writing C code to do it
>> may be necessary.
>> 
>> It's not really a very complicated system, it's just that things that are
>> obviously different in C (e.g. the R symbol name "gamma" versus the string
>> "gamma") look confusingly similar in R.
>> 
>> You might be able to figure this out by studying the result of str(x) (or
>> the low level .Internal(inspect(x))) for lots of different x objects, but I
>> don't think it's going to make sense unless you know what's going on behind
>> the curtain.
>> 
>> Duncan Murdoch
>> 
>> ______________________________________________
>> 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