[R] Passing on "groups" argument to xyplot within a plotting function

Felix Andrews felix at nfrac.org
Sat Aug 13 14:58:15 CEST 2011


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/



More information about the R-help mailing list