Ų  what does this piece of the code do: ~(.(\\1)<file:///\\1)>)

help("gsub")  should have the details.  In short, "\\1<file:///\\1>" it refers to the text
matching the 1st parenthesized subpattern in the pattern argument.  The
pattern was "_(mgkg|ugkg)" so for text "Zn_ugkg" the text matching the
first parenthesized subpattern is "ugkg".

You should try stepping through the code to see what each line does.  Just
set 'name' to a typical input and copy and paste the lines of the function
and examine the results.


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

From: Shane Carey [mailto:careyshan@gmail.com]
Sent: Thursday, April 04, 2013 1:55 AM
To: William Dunlap
Cc: r-help@r-project.org
Subject: Re: [R] Superscript

Hi William,

Thanks for this piece of code, it does the trick perfectly, what does this piece of the code do: ~(.(\\1)<file:///\\1)>)

in the following section:

bquoteExpr <- parse(text=gsub(pattern,
                                "~(.(\\1)<file:///\\1)>)",
                                name))[[1]]

Thanks again

On Wed, Apr 3, 2013 at 5:33 PM, William Dunlap <wdunlap@tibco.com<mailto:wdunlap@tibco.com>> wrote:
Are you trying to convert a column name like "Na_mgkg" to a plot label like Na (mg kg^-1) ?
If so you will have to use both string manipulation functions like gsub() and expression manipulating
functions like bquote().  E.g.,

f <- function (name)
{
   # add other suffices and their corresponding plotmath expressions to the list
   env <- list2env(list(mgkg = bquote(mg ~ kg^{-1}),
                        ugkg = bquote(mu * g ~ kg^{-1})),
                   parent = emptyenv())
   pattern <- paste0("_(", paste(objects(env), collapse="|"), ")")
   bquoteExpr <- parse(text=gsub(pattern,
                                 "~(.(\\1)<file:///\\1)>)",
                                 name))[[1]]
   # I use do.call() to work around the fact that bquote's first argument is not evaluated.
   do.call(bquote, list(bquoteExpr, env))
}

d <- data.frame("Na_mgkg"=1:10, "K_ugkg"=10:1)
plot(Na_mgkg ~ K_ugkg, data=d, xlab=f("K_ugkg"), ylab=f("Na_mgkg"))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>


> -----Original Message-----
> From: r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org> [mailto:r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org>] On Behalf
> Of Shane Carey
> Sent: Wednesday, April 03, 2013 8:02 AM
> To: r-help@r-project.org<mailto:r-help@r-project.org>
> Subject: [R] Superscript
>
> Hi,
> How do I write a superscript within gsub?
>
> I have the following: gsub("_mgkg",expression(paste("mg kg"^{-1})),names[1])
>
> Thanks
>
>
>
> --
> Shane
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org<mailto: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.



--
Shane

	[[alternative HTML version deleted]]

