[Rd] eval functions... (PR#668)

Douglas Bates bates@stat.wisc.edu
22 Sep 2000 16:29:52 -0500


aprasad@fs.fed.us writes:

> I am trying to convert some S-PLUS code to R (a tcl/tk application that uses
> R)...
> here is the error I got in R (but not in S-PLUS)...so I am wondering if it is a
> bug.
> 
> Eg., the foll. extract from a function runs fine in S-PLUS but gives the error:
> Error in x[[j]] : subscript out of bounds
> in R
> 
> code snippet .....
> 
>  attach(xd)
> 	 resp <- "iv802"
> 	pickedlist <- "BD + CEC + CLAY + ERODFAC + INCEPTSL"
> 	lmexp <- paste(resp, " ~ ", pickedlist,sep="")
>         tmpzx <- eval(lm(lmexp,data=xd))
> 
> Essentially, I want to do:
>  lm(iv802 ~ BD + CEC + CLAY + ERODFAC + INCEPTSL, data=xd)
> thru a function that has the parameters passed thru TclTk and Perl......

I think in both R and S-PLUS you would be better off using as.formula.

> as.formula("iv802 ~ BD + CEC + CLAY + ERODFAC + INCEPTSL")
iv802 ~ BD + CEC + CLAY + ERODFAC + INCEPTSL

You may want to use Bill Venables' favorite function do.call as well.  Try

resp <- "iv802"
pickedlist <- "BD + CEC + CLAY + ERODFAC + INCEPTSL"
tmpzx <- do.call("lm", 
          list(formula = as.formula(paste(resp, "~", pickedlist)),
               data = xd))

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._