I need to play games with an expression similar to the following one: print(xyplot(DepVar ~ Group|Covar, groups=Othergroup, data=mydf, pch = 18 ,main="Testcase",auto.key = TRUE)) The problem is that the formula argument (the first argument) an the groups argument are passed over from another program as strings. The formula argument does not pose problems, I can do as.formula("DepVar ~ Group|Covar") bit I could not find an equivalent technique for the groups parameter. as.expression(... does not work and neither does parse(text=... Is there a way of accomplishing what I need?
Try this: f <- function(fo, data, groups) { g <- do.call("xyplot", list(as.formula(fo), groups = as.name(groups), data)) print(g) } f("yield ~ variety | site", data = barley, groups = "year") On Tue, Sep 22, 2009 at 3:03 PM, Erich Neuwirth <erich.neuwirth at univie.ac.at> wrote:> I need to play games with an expression similar to the following one: > > print(xyplot(DepVar ~ Group|Covar, groups=Othergroup, > ? ? ? ?data=mydf, pch = 18 ,main="Testcase",auto.key = TRUE)) > > > The problem is that ?the formula argument (the first argument) > an the groups argument are passed over from another program as strings. > > The formula argument does not pose problems, > I can do as.formula("DepVar ~ Group|Covar") > bit I could not find an equivalent technique for the groups parameter. > as.expression(... does not work and neither does > parse(text=... > > Is there a way of accomplishing what I need? > > ______________________________________________ > R-help at 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. >
Thanks, that completely solves the problem. On Sep 22, 2009, at 10:27 PM, Gabor Grothendieck wrote:> Try parse(text=...): > > f <- function(fo, data, groups) { > g <- do.call("xyplot", list(as.formula(fo), > groups = parse(text = groups), data)) > print(g) > } > > f("yield ~ variety | site", data = barley, groups = "year") > > > > On Tue, Sep 22, 2009 at 4:20 PM, Erich Neuwirth > <erich.neuwirth at univie.ac.at> wrote: >> Thank you, this works for my example. >> On Sep 22, 2009, at 9:17 PM, Gabor Grothendieck wrote: >> >> f <- function(fo, data, groups) { >> g <- do.call("xyplot", list(as.formula(fo), groups = as.name >> (groups), data)) >> print(g) >> } >> >> But xyplot allows expressions for the groups parameter also, >> and using as.expression instead of as.name in your example does not >> work. >> What is happening? >> >> f <- function(fo, data, groups) { >> g <- do.call("xyplot", list(as.formula(fo), groups = as.expression >> (groups), >> data)) >> print(g) >> } >> >> f("yield ~ variety ~ site",barley,"year") >> produces a very nicely labeled but empty plot. >> >
On Tue, Sep 22, 2009 at 12:03 PM, Erich Neuwirth <erich.neuwirth at univie.ac.at> wrote:> I need to play games with an expression similar to the following one: > > print(xyplot(DepVar ~ Group|Covar, groups=Othergroup, > ? ? ? ?data=mydf, pch = 18 ,main="Testcase",auto.key = TRUE)) > > > The problem is that ?the formula argument (the first argument) > an the groups argument are passed over from another program as strings. > > The formula argument does not pose problems, > I can do as.formula("DepVar ~ Group|Covar") > bit I could not find an equivalent technique for the groups parameter.'groups' can be a formula too, e.g., xyplot(Sepal.Length ~ Petal.Length, iris, groups = ~Species) -Deepayan
Let us walk through my problem step by step: 1. print(xyplot(yield ~ variety | site,groups=year,data=barley)) 2. print(xyplot(as.formula("yield ~ variety | site"),groups=year,data=barley)) The above two statements work, the next one does not work 3. print(xyplot(as.formula("yield ~ variety | site"),groups=parse (text="year"),data=barley)) But the following one works 4. arglist<-list(as.formula("yield ~ variety | site"),groups=parse (text="year"),data=barley) print(do.call("xyplot",arglist)) why does 3 not work and 4 work? Is there a way of coercing the string "year" into something which can directly be used in 3 as the argument used with groups= ??? The next one print(xyplot(as.formula("yield ~ variety | site"),groups=eval(parse (text="year")),data=barley)) also works, but in this case the groups expression is evaluated before xyplot is applied, and I think this is bad style (at least, if not worse). On Sep 30, 2009, at 1:20 PM, Deepayan Sarkar wrote:> xyplot(Sepal.Length ~ Petal.Length, iris, groups = ~Species)