Fredrik Karlsson
2011-Aug-11 09:42 UTC
[R] Passing on "groups" argument to xyplot within a plotting function
An embedded and charset-unspecified text was scrubbed... Name: inte tillg?nglig URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110811/f20d8760/attachment.pl>
Felix Andrews
2011-Aug-13 12:58 UTC
[R] Passing on "groups" argument to xyplot within a plotting function
The problem is that xyplot tries to evaluate 'groups' in 'data' or in the formula environment. Your local function environment (where the variable named "groups" is defined) is neither of these. There are a couple of ways to get the evaluation to work out; here is one: pb <- list(F1 = 1:8, F2 = 1:8, Type = c('a','a','a','a','b','b','b','b')) foo <- function(x,data,groups, ...){ ccall <- quote(xyplot(x,data=data, ...)) ccall$groups <- substitute(groups) eval(ccall) } foo(F1 ~ F2, pb, groups = Type) Hope that helps -Felix On 11 August 2011 19:42, Fredrik Karlsson <dargosch at gmail.com> wrote:> Hi, > > I am constructing a plotting function that I would like to behave like > plotting functions within the lattice package. It takes a "groups" argument, > which I process, and then I would like to pass that argument on to the > xyplot function for the actual plotting. However, what ever I do, get an > error that the variable is missing. > > A short illustration: > > Given the data set > >> names(pb) > [1] "Type" ? ?"Sex" ? ? "Speaker" "Vowel" ? "IPA" ? ? "F0" ? ? ?"F1" > [8] "F2" ? ? ?"F3" > > and these test functions: > > TESTFUN <- function(x,data,groups){ > > ?xyplot(x,data=data,groups=groups) > } > > TESTFUN2 <- function(x,data,groups){ > > ?xyplot(x,data=data,groups=substitute(groups)) > } > > TESTFUN3 <- function(x,data,groups){ > ?groups <- eval(substitute(groups), data, environment(x)) > > ?xyplot(x,data=data,groups=groups) > } > > I fail to get "groups" to be passed on to xyplot correctly: > >> TESTFUN(F1 ~ F2,data=pb,groups=Type) > Error in eval(expr, envir, enclos) : object 'groups' not found >> TESTFUN2(F1 ~ F2,data=pb,groups=Type) > Error in prepanel.default.function(groups = groups, x = c(2280L, 2400L, ?: > ?object 'groups' not found >> TESTFUN3(F1 ~ F2,data=pb,groups=Type) > Error in eval(expr, envir, enclos) : object 'groups' not found > > Please help me understand what I am doing wrong. > > /Fredrik > > -- > "Life is like a trumpet - if you don't put anything into it, you don't get > anything out of it." > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Felix Andrews / ??? http://www.neurofractal.org/felix/