[R] expression, strsplit, ...

baptiste Auguié ba208 at exeter.ac.uk
Thu Jun 26 14:54:16 CEST 2008


OK, thanks to both of you for the clarifications. I guess part of my  
confusion came from the numerous functions and concepts involved in  
producing such labels:

- call vs string vs formula vs expression ...

- substitute, bquote, expression, "~", .(), ...

I take it as a good thing once you have some experience, as it  
probably gives several ways to cat a string. The "expense of clarity"  
in the use of "~" was useful for me complaining about the rather  
verbose syntax of some plotmath examples. I may try to produce some  
examples in the future for the R wiki to illustrate these particular  
annotation details.

Thanks again,

Baptiste



On 26 Jun 2008, at 12:07, Prof Brian Ripley wrote:

> On Thu, 26 Jun 2008, baptiste Auguié wrote:
>
>>
>> On 25 Jun 2008, at 19:45, Gabor Grothendieck wrote:
>>
>>> Try this:
>>> plot(1, xlab = ~ alpha / V * m^-3 * kg ^-2 * l^4)
>>
>>
>> Thanks, I would never have expected this code to work, this is a  
>> mystery to me! Actually, I thought xlab wanted an expression, but  
>> it seems to be happy with a formula.
>
> From ?plotmath
>
>      In most cases other language objects (names and calls) are  
> coerced
>      to expressions and so can also be used.
>
> A formula is a call:
>
>> mode(~ alpha / V * m^-3 * kg ^-2 * l^4)
> [1] "call"
>
> That was just GG saving himself a few key strokes at the expense of  
> clarity:
>
> plot(1, xlab = expression(alpha / V * m^-3 * kg ^-2 * l^4))
>
> is what this is coerced to.
>
>
>> Also, the handling of exponents is cleverer than I naively assumed.
>
> It is exactly as documented.
>
>> I think an example like the one above would make a nice addition  
>> to the plotmath documentation.
>
> I don't see what it adds to those already there.
>
>>
>> One small thing bothers me: can one use a similar syntax and use  
>> something like bquote to substitute a variable?
>
> You use substitute() to do that:
>
>> substitute(alpha *  aaa / V * m^-3 * kg ^-2 * l^4, list(aaa=a))
> alpha * " some text "/V * m^-3 * kg^-2 * l^4
>
>> say,
>>
>> a <- " some text "
>>
>>> plot(1, xlab = ~ alpha *  `a` / V * m^-3 * kg ^-2 * l^4)
>>
>> to produce,
>>
>>
>>> plot(1, xlab = ~ alpha * " some text " / V * m^-3 * kg ^-2 * l^4)
>>
>> but I can't get either bquote, deparse, substitute, "``" to  
>> produce the desired substitution in this formula.
>
> You can't 'substitute' in a formula, but you can in the RHS of a  
> formula, form[[2]] in your case.
>
>> Many thanks,
>>
>> baptiste
>>
>>
>>
>>
>>> On Wed, Jun 25, 2008 at 1:06 PM, baptiste Auguié  
>>> <ba208 at exeter.ac.uk> wrote:
>>>> DeaR list,
>>>> I'm a bit lost in the behavior of substitute and co.
>>>> I often use fairly long axis labels in my graphs (long to write,  
>>>> that is).
>>>> Typically, they would contain some greek letters and units with  
>>>> exponents,
>>>> as in:
>>>>
>>>>>       xlab=expression(paste("text ", alpha, " / ", V,".", m^ 
>>>>> {-3}, ".",
>>>>> kg^{-2}, ".", l^{4}))
>>>> To make this a bit prettier, I've attempted to mimic the  
>>>> behavior of the
>>>> SIstyle latex package which defines a macro that cleverly parses an
>>>> expression for units, exponents, etc. My code fails in putting  
>>>> together the
>>>> unit, as seen in the example below:
>>>>> makeUnits <- function(expr){ # formats the units
>>>>> units.list <- strsplit(expr, "[[:blank:]]", perl=F)
>>>>> expr.list <- strsplit(unlist(units.list), "\\^", perl=T)
>>>>> units <- unlist(lapply(expr.list, function(unit) {
>>>>>               if (length(unit) == 2)
>>>>> paste(unit[1],"^{",unit[2],"}",sep="")
>>>>>               else paste(unit,sep="")
>>>>>               }))
>>>>>       cat(units, sep=".")
>>>>> }
>>>>> expr <- "V m^-3 kg^-2 l^4"
>>>>> makeUnits(expr) # this works, and produces:
>>>> # V.m^{-3}.kg^{-2}.l^{4}
>>>>> silab <- function(..., units=NULL){ # creates a valid  
>>>>> expression for xlab
>>>>> and co.
>>>>>       dotCalls <- substitute(list(...))
>>>>>       nArgs <- length(dotCalls)
>>>>> if (!is.null(units))    myUnits <- makeUnits(units)
>>>>> if (!is.null(units)) return(substitute(paste(..., " / ",   
>>>>> myUnits)))
>>>>> if (is.null(units)) return(substitute(paste(...)))
>>>>> }
>>>>> silab("text", alpha,  units = "V m^-3 kg^-2 l^4") # the result is
>>>>> obviously not what I want
>>>>> par(mfrow=c(2, 1)) # comparison of the desired output and the  
>>>>> current one
>>>>> plot(1:10, 1:10,
>>>>>       xlab=silab("text ", alpha, units = "V m^-3 kg^-2 l^4"),
>>>>>       ylab=silab("simple text"))
>>>>> plot(1:10, 1:10,
>>>>>       xlab=expression(paste("text ", alpha, " / ", V,".", m^ 
>>>>> {-3}, ".",
>>>>> kg^{-2}, ".", l^{4})),
>>>>>       ylab="simple text")
>>>> Any thoughts welcome!
>>>> Sincerely,
>>>> baptiste
>>>> _____________________________
>>>> Baptiste Auguié
>>>> Physics Department
>>>> University of Exeter
>>>> Stocker Road,
>>>> Exeter, Devon,
>>>> EX4 4QL, UK
>>>> Phone: +44 1392 264187
>>>> http://newton.ex.ac.uk/research/emag
>>>> http://projects.ex.ac.uk/atto
>>>> ______________________________________________
>>>> 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.
>>
>> _____________________________
>>
>> Baptiste Auguié
>>
>> Physics Department
>> University of Exeter
>> Stocker Road,
>> Exeter, Devon,
>> EX4 4QL, UK
>>
>> Phone: +44 1392 264187
>>
>> http://newton.ex.ac.uk/research/emag
>> http://projects.ex.ac.uk/atto
>>
>> ______________________________________________
>> 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.
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

_____________________________

Baptiste Auguié

Physics Department
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
http://projects.ex.ac.uk/atto



More information about the R-help mailing list