Names of Greek letters stored as character strings; plotmath.
On Sat, May 19, 2012 at 2:16 PM, Paul Johnson <pauljohn32 at gmail.com> wrote:
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))
Actually, I was pointing out that that expression was as good as a longer expression that was posted and it always works (not just sometimes) if one interprets the problem as supplying the character representation of a plotmath expression in xNm -- this seems the logical generalization given that the example was the character representation of a plotmath expression.
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com