[R] Evaluating expresssions as parameter values

Peter Ehlers ehlers at ucalgary.ca
Wed Sep 23 13:25:34 CEST 2009


Can't you just use get()? What am I missing?

   f <- function(fo, data, groups) {
       g <- xyplot(as.formula(fo), groups = get(groups), data)
       print(g)
   }
   f("yield ~ variety | site", data = barley, groups = "year")

Peter Ehlers

Erich Neuwirth wrote:
> 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.
>>>
>>
> 
> ______________________________________________
> 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.
> 
>




More information about the R-help mailing list