[R] Non-interactive passing of quoted variables (ggplot, plyr, subset, transform, etc)

hadley wickham h.wickham at gmail.com
Mon Dec 1 18:29:36 CET 2008

On Mon, Dec 1, 2008 at 11:06 AM, Vitalie Spinu <vitosmail at rambler.ru> wrote:
> Hello Everyone,
> May be a silly question.
> How to pass programmatically variables which are not known in advance and
> are quoted? Variables are quoted implicitly in  functions like "subset" and
> "transform" and explicitly in ggplot and plyr.
> For instance I would like to have something like this:
> myfunc<-functon(mydata,X) subset(mydata,X>4)
> myfunc<-function(mydata,X,Y) {ggplot(mydata,aes(x=X,y=Y))+geom_points()}

For subset, just do it by hand:

mydata[mydata[[X]] > 4, ]

For ggplot, see aes_string:

aes_string(x = X, y = Y)
which will do what you want

For plyr, you can use a character vector:

ddply(mtcars, c("cyl"), colwise(mean))
ddply(mtcars, c("cyl ^ 2", "vs - am"), colwise(mean))

In general you need to use substitute or bquote.



More information about the R-help mailing list