[R] a question about function eval()
Uwe Ligges
ligges at statistik.uni-dortmund.de
Thu Feb 24 12:59:45 CET 2005
Feng Chen wrote:
> Hi,
>
> I have a question about the usage of eval(). Wonder if any experienced user can help me out of it.
>
> I use eval() in the following function:
> semireg.pwl <- function(coef.s=rnorm(1),coef.a=rnorm(1),knots.pos=knots.x,knots.ini.val=knots.val){
> knotn <- length(knots.pos)
> def.par.env <- sys.frame(1)
> print(def.par.env)
> print(environment(coef.s))
> tg <- eval( (parse(text=
> paste(
> "function(coef.sex=coef.s,coef.age=coef.a,",
> paste("knot.val.",1:knotn,"=knots.ini.val[",1:knotn,"]",sep="",collapse=","),
> ")",
> "{\n print(sys.frame());print(coef.sex)\n y <- c(",
> paste("knot.val.",1:knotn,sep="",collapse=","),
> ")\n",
> " -loglikelihood(coef.sex,coef.age,knots.pos,y)\n}",
> sep="",collapse=""
> )
> )
> )
> ,envir=def.par.env)
Why do you want to use eval(parse(.....))? That's not sensible in this
case. You can either alculate the stuff doirectly or define a function
instead....
Uwe Ligges
> print(tg())
> print(coef.s)
> print(sys.frame(1))
> mle(tg)
> }
>
> But when I ran semireg.pwl(), I got correct value for tg(), but an message
> "Error in eval(expr, envir, enclos) : Object "coef.s" not found"
> for mle(tg). I just don't know how to make the environment variable visible for mle().
>
> Thanks a lot!
>
> Feng
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list