Bill, Thanks so much for the tips. For the project that I'm doing now, I don't need to worry about bullet-proofing it too much, but this is definitely educational and will help tons when the next one comes around.
Cheers, eric On Sun, Jul 7, 2013 at 10:22 AM, William Dunlap <wdun...@tibco.com> wrote: > > x.lab <- gsub(",","*symbol(\"\\\\54\")*", x.lab) > > Wouldn't using just > "*\",\"*" > instead of > "*symbol(\"\\\\54\")*" > as the replacement do the same thing? > To me it is simpler to understand. > > Note that this fails if the comma is the first or last > character in the input because '*something*' is > not a valid expression. Another problem is that '**' > is parsed the same as "^", so "a**d" is displayed as > "a Delta superscript Delta d". One way to deal with that > problem is to strip possible '*'s from the ends of > x.lab and convert '**'s to '*'s before giving it to the parser. > x.lab <- gsub("^\\*|\\*$", "", x.lab) > x.lab <- gsub("\\*\\*", "*", x.lab) > as in > f1 <- function (x.lab) > { > x.lab <- gsub("\\*", "*Delta*", x.lab) > x.lab <- gsub(",", "*\",\"*", x.lab) > x.lab <- gsub("^\\*|\\*$", "", x.lab) > x.lab <- gsub("\\*\\*", "*", x.lab) > parse(text = x.lab, keep.source = FALSE) > } > where the code in your mail corresponds to the function f0: > f0 <- function (x.lab) > { > x.lab <- gsub("\\*", "*Delta*", x.lab) > x.lab <- gsub(",", "*symbol(\"\\\\54\")*", x.lab) > parse(text = x.lab, keep.source = FALSE) > } > > Another approach is not to turn the commas into strings, but > turn anything that is not a '*' into a string. Then you don't have > to change your code when you discover that the inputs might > contain semicolons or something else. > f2 <- function (x.lab) > { > x.lab <- gsub("([^*]+)", " \"\\1\" ", x.lab) > # I put spaces around things to make it a little more readable; > # they may not be very readable in some fonts. > x.lab <- gsub("\\*", " * Delta * ", x.lab) > x.lab <- gsub("^ \\*|\\* $", "", x.lab) > x.lab <- gsub("\\* \\*", "*", x.lab) > parse(text = x.lab, keep.source = FALSE) > } > > Use it as in > dotchart(1:4, labels=f2(c("***d", ",,c*", "a,*d", "****"))) > This code gets ugly pretty quickly so you should bury it in a function. > > 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 Eric Archer - NOAA Federal > > Sent: Saturday, July 06, 2013 10:55 PM > > To: Duncan Mackay; r-help-r-project.org > > Subject: Re: [R] Transferring commas in character vector to expression > > > > Duncan, > > > > Thanks! That was the tip I needed. With that, I was able to get this to > > work perfectly: > > > > x.lab <- c("a*a", "bbb", "c,cc*c", "d,dd") > > x.lab <- gsub("\\*", "*Delta*", x.lab) > > x.lab <- gsub(",", "*symbol(\"\\\\54\")*", x.lab) > > dotchart(1:length(x.lab), labels = parse(text = x.lab)) > > > > > > > > On Sat, Jul 6, 2013 at 9:38 PM, Duncan Mackay <mac...@northnet.com.au > >wrote: > > > > > Eric > > > > > > How does this look - (you might have to add a few symbol("\54") where > > > needed for me this give a comma on windows7 ver 3.1 > > > > > > xyplot(1:4 ~ 1:4, scales = list(x=list(at = 1:4, labels = > > > > > > c(expression(a*a),expression(bbb),expression(c*Delta*cc*c),expression(d*Delta*symbol( > > "\54")*dd) > > > ) ) ) ) > > > > > > I mostly use lattice and have forgotten how to do labels in basic plot > so > > > have used lattice. should be similar and you can modify to suit. > > > > > > It was trial and error in going up through the numbers from about 38 > > > > > > > > > Duncan > > > > > > At 11:57 7/07/2013, you wrote: > > > > > > Duncan, > > > > > > Thanks for the suggestion, but that won't work for my situation. I'm > > > trying to use a character vector to label some axis ticks. There are > some > > > elements in the vector that have either a comma, or both Greek symbols > and > > > a comma, like the the third and fourth elements in x.lab below: > > > > > > > x <- 1:4 > > > > x.lab <- c("a*a", "bbb", "c,cc*c", "d,dd") > > > > x.lab <- gsub("\\*", "*Delta*", x.lab) > > > > x.lab <- parse(text = x.lab) > > > Error in parse(text = x.lab) : <text>:3:2: unexpected ',' > > > 2: bbb > > > 3: c, > > > ^ > > > > dotchart(x, labels = x.lab) > > > > > > The root problem that I'm stumped on is how to either: > > > 1) insert a comma into an expression and have it be read as a valid > > > character, or > > > 2) replace the comma in the character string with 'list(a, b, c)' as in > > > the help for plotmath and have it interpreted correctly. > > > > > > Cheers, > > > eric > > > > > > > > > On Sat, Jul 6, 2013 at 3:33 PM, Duncan Mackay <mac...@northnet.com.au> > > > wrote: > > > Hi Eric > > > > > > I have not been following the thread but following on what David has > said > > > on previous occasions > > > > > > try for example > > > > > > plot(1,1, ylab = expression("aa aaa,aa bb"*Delta*"b cccc"*Delta*"cc, > c") ) > > > > > > Below is from a partly saved previous post of David's several months > ago > > > which may give you some ideas > > > > > > DATA_names<-c( > > > "A mg kg", > > > "B mg kg", > > > "C mg kg", > > > "D mg kg", > > > "E mg kg", > > > "F mg kg", > > > "G mg kg", > > > "H mg kg") > > > > > > pos <- barplot(1:length(DATA_names)) > > > text(x=pos,y=-1, xpd=TRUE, srt=45, > > > labels= sapply( gsub("mg kg", "(mg kg)^-1", > > > DATA_names), > > > as.expression)) > > > > > > HTH > > > > > > Duncan > > > > > > > > > Duncan Mackay > > > Department of Agronomy and Soil Science > > > University of New England > > > Armidale NSW 2351 > > > Email: home: mac...@northnet.com.au > > > > > > > > > > > > > > > At 07:47 6/07/2013, you wrote: > > > I'm trying to format a given character vector as an expression with > Greek > > > symbols to be used in labeling axis ticks. Thanks to some help from > David > > > Winsemius, I've learned how to make the substitution and place the > Greek > > > symbols in, however I've run into another problem: Some of my labels > have > > > commas in them, so when the parse command is executed, there is an > > > unexpected symbol error. For example: > > > > > > > x <- c("aa", "aaa,aa", "bb*Delta*b", "cccc*Delta*cc,c") > > > > parse(text = x) > > > Error in parse(text = x) : <text>:2:4: unexpected ',' > > > 1: aa > > > 2: aaa, > > > ^ > > > > > > I've tried various iterations of wrapping the commas in interior quotes > > > ("aaa\",\"aa"), but then the error shifts to the quote. I see in > plotmath > > > that 'list(a,b,c)' gives me comma separated values, but I haven't been > able > > > to work out how to get this construction for elements that have a > comma. > > > > > > Is this possible? > > > > > > -- > > > > > > Eric Archer, Ph.D. > > > Southwest Fisheries Science Center > > > NMFS, NOAA > > > 8901 La Jolla Shores Drive > > > La Jolla, CA 92037 USA > > > 858-546-7121 (work) > > > 858-546-7003 (FAX) > > > > > > Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics > > > ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp > > > > > > "The universe doesn't care what you believe. > > > The wonderful thing about science is that it > > > doesn't ask for your faith, it just asks > > > for your eyes." - Randall Munroe > > > > > > "Lighthouses are more helpful than churches." > > > - Benjamin Franklin > > > > > > "...but I'll take a GPS over either one." > > > - John C. "Craig" George > > > > > > [[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. > > > > > > > > > > > > > > > > > > > > > -- > > > > > > Eric Archer, Ph.D. > > > Southwest Fisheries Science Center > > > NMFS, NOAA > > > 8901 La Jolla Shores Drive > > > La Jolla, CA 92037 USA > > > 858-546-7121 (work) > > > 858-546-7003 (FAX) > > > > > > Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics > > > ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp > > > > > > "The universe doesn't care what you believe. > > > The wonderful thing about science is that it > > > doesn't ask for your faith, it just asks > > > for your eyes." - Randall Munroe > > > > > > "Lighthouses are more helpful than churches." > > > - Benjamin Franklin > > > > > > "...but I'll take a GPS over either one." > > > - John C. "Craig" George > > > > > > > > > > > > -- > > > > Eric Archer, Ph.D. > > Southwest Fisheries Science Center > > NMFS, NOAA > > 8901 La Jolla Shores Drive > > La Jolla, CA 92037 USA > > 858-546-7121 (work) > > 858-546-7003 (FAX) > > > > Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics > > ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp > > > > "The universe doesn't care what you believe. > > The wonderful thing about science is that it > > doesn't ask for your faith, it just asks > > for your eyes." - Randall Munroe > > > > "Lighthouses are more helpful than churches." > > - Benjamin Franklin > > > > "...but I'll take a GPS over either one." > > - John C. "Craig" George > > > > [[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. > -- Eric Archer, Ph.D. Southwest Fisheries Science Center NMFS, NOAA 8901 La Jolla Shores Drive La Jolla, CA 92037 USA 858-546-7121 (work) 858-546-7003 (FAX) Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp "The universe doesn't care what you believe. The wonderful thing about science is that it doesn't ask for your faith, it just asks for your eyes." - Randall Munroe "Lighthouses are more helpful than churches." - Benjamin Franklin "...but I'll take a GPS over either one." - John C. "Craig" George [[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.