Skip to content
Prev 4361 / 63421 Next

eval functions... (PR#668)

On Fri, 22 Sep 2000 aprasad@fs.fed.us wrote:

            
A tracback helps ...
I don't understand why that would work in S-PLUS!  You apply eval to
expressions or calls, but lm(mexp,data=xd) is not a call. Probably S-PLUS
has an implicit as.formula in its version.

The essence can be seen in
Error in x[[j]] : subscript out of bounds
[1] "eval(expr, envir, enclos)"                                        
[2] "eval(attr(formula, \"variables\"), data, sys.frame(sys.parent()))"
[3] "model.frame.default(formula = y ~ x, drop.unused.levels = TRUE)"  
[4] "model.frame(formula = y ~ x, drop.unused.levels = TRUE)"          
[5] "eval(expr, envir, enclos)"                                        
[6] "eval(mf, sys.frame(sys.parent()))"                                
[7] "lm(formula = y ~ x)"                                              

The point is that the formula argument should be a formula, not a 
character string, so this is not a bug per se.

There are lots of correct ways to do this.  Perhaps the simplest is

lm(as.formula(lmexp), data=xd)

but that will record the call as 

lm(formula = as.formula(lmexp), data = xd)

To avoid that, if you need to, use something like

do.call("lm", list(formula = as.formula(lmexp), data=xd))

(There are lots more ways too, including using parse or substitute.)