[Rd] Questions about calls and formulas

Douglas Bates dmbates at gmail.com
Mon Aug 22 23:52:08 CEST 2005


On 8/22/05, Erich Neuwirth <erich.neuwirth at univie.ac.at> wrote:
> I am trying to adapt boxplot.formula (in graphics) to accept an
> additional parameter, weights.
> I already managed to adapt boxplot.default to do this.
> 
> boxplot.formula prepares the data for a call to boxplot.default and to
> achieve that does the following: It takes a formula like
> 
>     x~g*h
> 
> as the first argument, and then by using
> 
>     m <- match.call(expand.dots = FALSE)
> 
> saves the call. It transforms the call
> 
>     m$na.action <- na.action # force use of default for this method
>     m[[1]] <- as.name("model.frame")
> 
> and then  evaluates the modified call
>     mf <- eval(m, parent.frame())
> 
>     print(m)
> gives
>     model.frame(formula = x ~ g * h)
> 
> Then it uses components of mf for the call to boxplot.default.
> 
> m has a component m$formula containing the parsed model formula.
> mode(m$formula) is "call".
> In our case, deparse(m$formula) gives a string representation of the
> formula: "x~g*h".
> I want to replace the response variable (in our case x) by the weights
> variable, which in the string expression can be done easily with
> strsplit and paste. Then I need to reconvert the modified string to a call.
> 
> So I create newmodelstring<-"weights~g*h" and try
> 
>     m$formula<-as.call(parse(newmodelstring))
> 
>     print(m)
> gives
>     model.frame(formula = weights ~ g * h())
> 
> 
> When I try to evaluate the modified m this does not work. When I try to
> evaluate m with this modification I get
> 
> Error in model.frame(formula = weights ~ g * h()) :
>         attempt to apply non-function
> 
> Is there a way to get rid of the empty parentheses at the
> end of the formula? I think then my code could work.
> 
> --
> Erich Neuwirth, Didactic Center for Computer Science
> University of Vienna
> Visit our SunSITE at http://sunsite.univie.ac.at
> Phone: +43-1-4277-39902 Fax: +43-1-4277-9399
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

I think the preferred way to do this is using substitute although
formulas are a bit tricky in that you need to eval them after the
substitution to make sure that the object has class "formula".


> (foo <- eval(substitute(x ~ g * h, list(x = as.name("weights")))))
weights ~ g * h
> class(foo)
[1] "formula"



More information about the R-devel mailing list