[R] Names of Greek letters stored as character strings; plotmath.
William Dunlap
wdunlap at tibco.com
Sun May 20 00:31:01 CEST 2012
Paul Johnson wrote
> 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))))
>
> It appears to me that 2/3 of the as.name usages are not needed, at
> least not in R 2.15 on Debian Linux.
>
> This works just as well
>
> plot(0, main=bquote(.(x)^.(as.name(power))/.(y)))
The return value of bquote is different in those 2 cases (with and without
the as.name):
> bquote(.(as.name(x))^.(as.name(power))/.(as.name(y)))
`Waist Size (cm)`^gamma/`Weight (kg)`
> bquote(.(x)^.(as.name(power))/.(y))
"Waist Size (cm)"^gamma/"Weight (kg)"
However, plotmath doesn't print the quotes around a string like
"Weight (kg)", nor the backticks around a weird name like `Weight (kg)`,
so you don't notice the difference when using bquote's output for plotmath.
If you were constructing a formula for lm() then you would notice the difference.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: Paul Johnson [mailto:pauljohn32 at gmail.com]
> Sent: Saturday, May 19, 2012 11:16 AM
> To: William Dunlap
> Cc: r-help
> Subject: Re: [R] Names of Greek letters stored as character strings; plotmath.
>
> On Sat, May 19, 2012 at 11:07 AM, William Dunlap <wdunlap at tibco.com> wrote:
> > 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
> >
> Bill's point is on the money here. We should aim to teach one way that
> works always. But finding that one way is the hard part for me.
>
> Lately, I'm bothered that R (or computers in general?) allows too many
> ways to do the same thing that work SOME of the time. Without a very
> deep understanding of the terminology, users are bewildered.
>
> Go read ?plotmath. Do we see "as.name"? NO. Why does the idiom
> expression(paste()) work as well? (Don't answer, that's a rhetorical
> question).
>
> There are too many ways to do the same thing. Or, well, too many of us
> know one way that works sometime and don't find out it doesn't always
> work until, as Bill says, it is buried in the bottom of some big
> function that behaves erratically.
>
> Gabor notes this works (sometimes):
>
> plot(0, xlab = parse(text = xNm))
>
> Bert observes this works (sometimes)
>
> xnm <- quote(gamma)
> ## makes xnm the name gamma not the string "gamma"
> plot(0,xlab = bquote( .(xnm))
>
> Bill observes this works:
> xnm <- "gamma"
> plot(0,xlab = bquote(.(as.name(xnm))))
>
> In line with that, Bill suggests:
>
> 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))))
>
> It appears to me that 2/3 of the as.name usages are not needed, at
> least not in R 2.15 on Debian Linux.
>
> This works just as well
>
> plot(0, main=bquote(.(x)^.(as.name(power))/.(y)))
>
>
> Is there any important reason to wrap x and y in this command with as.name?
>
> It does work, maybe I be in the general habit of wrapping as.name
> around variables in bquotes? I'm more confused.
>
> The good new for me is that I cannot find any replacements for the
> usage of "as.name" in that particular expression. So there is just one
> way.
>
> Oh, wait, I spoke too soon. as.symbol is identical to as.name.
>
> plot(0, main=bquote(.(x)^.(as.symbol(power))/.(y)))
>
> And then, logically, this works:
>
> plot(0, main=bquote(.(x)^.(as.vector(power, "symbol"))/.(y)))
>
> I personally think "as.symbol" is more likely to be understood by
> students, I may stick to that. So I think the most succinct, best
> approach is
>
>
> plot(0, main=bquote(.(x)^.(as.symbol(power))/.(y)))
>
> or perhaps the most consistent-looking strategy is:
>
> power <- as.symbol("gamma") ; x <- "Waist Size (cm)" ; y <- "Weight (kg)"
> plot(0, main=bquote(.(x)^.(power)/.(y)))
>
> Speaking of commands that have identical results and exist separately
> for no apparently good reason:
>
> library(help=rockchalk)
>
> help(package=rockchalk)
>
> pj
>
> >
> >> -----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.
> >
> > ______________________________________________
> > 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.
>
>
>
> --
> Paul E. Johnson
> Professor, Political Science Assoc. Director
> 1541 Lilac Lane, Room 504 Center for Research Methods
> University of Kansas University of Kansas
> http://pj.freefaculty.org http://quant.ku.edu
More information about the R-help
mailing list