On Thu, Apr 4, 2013 at 4:39 AM, Shane Carey <careys...@gmail.com> wrote:
> Hi William, > > for (i in one:length(DATA_names)) > if ((grepl("_",DATA_names[i]))=="TRUE") > DATA_names[i]<-f(DATA_names[i])) > > I keep getting an error saying: incompatible types (from symbol to > character) in subassignment type fix > > I would urge you not to obliterate DATA_names by writing on top of itself. That creates bugs that are really tough to solve. Remember that a character variable is not the same thing as an expression. I mean, an expression is generally a list structure, not interchangeable with a character string. If that f() function is creating expressions, and you are trying to obliterate a character vector with expressions, you will get errors, as you have seen. I'm pretty sure that to catch the results of a function that creates expressions, your receiver object has to be a list. My first try would be myList <- vector("list", length(DATA_names)) for (i in one:length(DATA_names)) if ((grepl("_", DATA_names[i])) == "TRUE") myList[[i]] <- f(DATA_names[i])) I copied your code to my system and found I couldn't run it because you had one more paren than was needed and the word "one" was undefined. But after fixing that, here's evidence my way works: Note I inserted some spaces in your code, since it is inhumane to smash together all those letters around a poor little <- :). DATA_names<-c("A_mgkg","B_mgkg","C_mgkg","D_mgkg","E_mgkg","F_mgkg","G_mgkg","H mgkg") 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))", 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)) } myList <- vector("list", length(DATA_names)) for (i in 1:length(DATA_names)) if ((grepl("_", DATA_names[i])) == "TRUE") myList[[i]] <- f(DATA_names[i]) myList > myList [[1]] A ~ (mg ~ kg^{ -1 }) [[2]] B ~ (mg ~ kg^{ -1 }) [[3]] C ~ (mg ~ kg^{ -1 }) [[4]] D ~ (mg ~ kg^{ -1 }) [[5]] E ~ (mg ~ kg^{ -1 }) [[6]] F ~ (mg ~ kg^{ -1 }) [[7]] G ~ (mg ~ kg^{ -1 }) [[8]] NULL I asked a very similar question here last year and got many interesting suggestions. I was so fascinated I wrote a little programming vignette called Rchaeology in my package "rockchalk". As soon as you settle on the most direct route from start to end, please post, I might learn new tricks. In your case, I've been wondering if you ought to edit the string before it becomes an expression, or whether you should edit the expression after. I needed a correct expression to start, though. This writes the correct thing in the middle of the graph. Correct? xcorrect <- expression(X ~~ "mg kg"^{-1}) plot(1:10, 1:10, type ="n") text(5, 5, xcorrect) Supposing that's correct, watch this. mgkg <- quote("mg kg"^{-1}) text(4, 2, bquote(A ~~ .(mgkg))) text(4, 3, bquote(B ~~ .(mgkg))) text(4, 6, bquote(C ~~ .(mgkg))) I've got the "units" captured in mgkg, and then when I want to draw that into and expression, i used bquote. substitute works as well, maybe more clear. text(3, 8, substitute(B ~~ myUnit, list(myUnit = mgkg))) In any case, I thought this was a fun question. pj > Have you any ideas on how to get around this, thanks again for your help, > much appreciated. > Cheers > > > On Wed, Apr 3, 2013 at 5:33 PM, William Dunlap <wdun...@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))", > > 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 > > > > > > > -----Original Message----- > > > From: r-help-boun...@r-project.org [mailto: > r-help-boun...@r-project.org] > > On Behalf > > > Of Shane Carey > > > Sent: Wednesday, April 03, 2013 8:02 AM > > > To: 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 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]] > > ______________________________________________ > 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. > -- 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 [[alternative HTML version deleted]] ______________________________________________ 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.