2

In the code below inA in third line from the bottom of the function is not recognized/evaluated properly. I get

Error in parse(text = inA) : object 'inA' not found".  

inA is recognized just fine in the two other lines above that where it is used. I have tried a LOT of permutations.

gcplot2 <- function (inraw,inA,inB){
   inwork   <- ddply(inraw,
                     .( eval(parse(text=inA)), eval(parse(text=inB)),BestYr),
                     summarize,  
                     cases=sum(Cases,na.rm=TRUE), 
                     pop=sum(Pop,na.rm=TRUE), 
                     rate=round(100000*cases/pop,2))
   names(inwork)[1] <- inA 
   names(inwork)[2] <- inB

   #problem "inA" is here
   x <- ggplot(inwork,aes(BestYr,rate, color=eval(parse(text=inA))))  

   x <- x + geom_line(size=1.5) + facet_wrap(as.formula(paste0("~ ",inB)))
   print(x)
}

gcplot2(inraw=gc.full,"NewRace","Region")

Here is a small sample of the data frame that I hope can be used for a "reproducible example".

dput(temp2)
structure(list(LHJ = c("SACRAMENTO", "YOLO", "SAN BENITO", "COLUSA", 
"STANISLAUS", "SAN DIEGO", "SHASTA", "TULARE", "MONTEREY", "KERN"
), BestYr = c(2010L, 2010L, 2010L, 2012L, 2012L, 2012L, 2011L, 
2011L, 2010L, 2010L), Sex = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 1L, 1L), .Label = c("F", "M"), class = "factor"), RaceEth = structure(c(3L, 
4L, 6L, 2L, 6L, 4L, 4L, 2L, 4L, 4L), .Label = c("A", "B", "H", 
"O", "U", "W"), class = "factor"), AgeGrp = structure(c(1L, 4L, 
5L, 2L, 7L, 2L, 4L, 2L, 3L, 1L), .Label = c("0-9", "10-14", "15-19", 
"20-24", "25-29", "30-34", "35-44", "45+", "Unk"), class = "factor"), 
Cases = c(NA, 0, 0, 0, 15.652173913, NA, 0, 0, 0, 0), Pop = c(32752.30608, 
538.17138648, 444.83561193, 11.107216039, 14186.950585, 5486.3069863, 
338.26814356, 245.3890448, 535.23711331, 2278.6798429), NewRace = c("Hispanic", 
"Other", "White", "Black", "White", "Other", "Other", "Black", 
"Other", "Other"), Region = structure(c(6L, 6L, 3L, 5L, 3L, 
8L, 5L, 3L, 2L, 3L), .Label = c("Bay Area", "Central Coast", 
"Central Inland", "Los Angeles", "Northern", "Sacramento Area", 
"San Francisco", "Southern"), class = "factor")), .Names = c("LHJ", 
"BestYr", "Sex", "RaceEth", "AgeGrp", "Cases", "Pop", "NewRace", 
"Region"), row.names = c(41377L, 67523L, 42571L, 7418L, 59857L, 
45051L, 54102L, 64260L, 32612L, 17538L), class = "data.frame")
joran
  • 169,992
  • 32
  • 429
  • 468
  • 2
    Sorry if I didn't state the question well. This is the first time I have used stack overflow. I quickly received 2 negative votes, so I must have stated the question poorly. Any suggestions? I have used R for many many years, and have "researched" this question for over an hour. I am likely making a dumb mistake... – user3629863 May 12 '14 at 19:49
  • A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would help a whole lot. – Ben Bolker May 12 '14 at 20:07
  • `?aes_string` may be useful too. – Ben Bolker May 12 '14 at 20:09
  • Ben- thank you so much. aes_string works, using the code: x <- ggplot(inwork,aes_string(x="BestYr",y="rate", color= inB)) x <- x + geom_line(size=1.5) + facet_wrap(inA) print(x) does this seem like the proper usage also, so my usage of eval(parse(text=inA)) in my original code seem ok, or is there cleaner way to do that? Thanks for any reply. – user3629863 May 12 '14 at 21:13
  • Ben- thank you so much. aes_string works, using the code: x <- ggplot(inwork,aes_string(x="BestYr",y="rate", color= inB)) x <- x + geom_line(size=1.5) + facet_wrap(inA) print(x) does this seem like the proper usage also, so my usage of eval(parse(text=inA)) in my original code seem ok, or is there cleaner way to do that? Thanks for any reply. – user3629863 May 12 '14 at 21:19
  • `eval(parse(...))` is almost always a mistake. If you're talking about the usage in `ddply`, I think you want `.variables=c(inA,inB,"BestYr")` (i.e. specify it via a character vector rather than trying to do it via `as.quoted` names) – Ben Bolker May 12 '14 at 21:37
  • 1
    (1) The question is much improved now, do note the formatting techniques for next time, (2) `aes_string` exists specifically because `eval(parse(...))` is almost always a bad idea. See `library(fortunes); fortune(106)`. (3) Please feel free to write what worked for you up in an answer below, and then (after a short waiting period) accept it. – joran May 12 '14 at 21:38
  • oops, I went ahead and answered. probably should have waited. – Ben Bolker May 12 '14 at 21:44

1 Answers1

0

I would do this as follows, with aes_string (and specifying the ddply variables via a character vector rather than .()):

gcplot2 <- function (inraw,inA,inB){
    require("plyr")
    require("ggplot2")
    inwork   <- ddply(inraw,
                     c(inA, inB,"BestYr"),
                     summarize,  
                     cases=sum(Cases,na.rm=TRUE), 
                     pop=sum(Pop,na.rm=TRUE), 
                     rate=round(100000*cases/pop,2))
    x <- ggplot(inwork,aes(BestYr,rate)) +
        geom_line( aes_string(color=inA),size=1.5) +
            facet_wrap(as.formula(paste0("~ ",inB)))
    x
}
gcplot2(inraw=gc.full,"NewRace","Region")

I get warnings, but I think that's due to using a tiny subset of the data ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453