[R] eval in correct frame?

Christian Hoffmann christian.hoffmann at wsl.ch
Thu Nov 25 11:17:28 CET 2004


I am trying, without success, to find out how to formulate correctly the 
parameters of "eval".

My code snippet looks like:

proutside <- function(txt) {
   cat("\n",txt,"\n");
   print(eval(parse(text = txt)))
}

vari <- function(Ob) {
   prininside <- function(txt) {
     cat("\n",txt,"\n");
     print(eval(parse(text = txt)))
   }
   prininside("'inside'; 2*Ob")
   proutside("'outside'; 2*Ob")
}
Obs <- matrix(rnorm(9),nrow=3,ncol=3)
 > vari(Obs)

  'inside'; 2*Ob
           [,1]     [,2]     [,3]
[1,] -1.566331 -1.98257 0.127522
[2,]  3.161932 -4.88416 2.355412
[3,] -0.763759  2.33552 2.165868

  'outside'; 2*Ob
Error in eval(expr, envir, enclos) : Object "Ob" not found
 >

My aim is to be able to use a function of type "proutside" as deep 
inside a function within a function... as I wish. I suppose, that the 
call of "eval" within "proutside" should have "envir = parent.frame(), 
enclos = ", but I cannot figure that out correctly.

Thank for any help.
Christian

-- 
Dr.sc.math.Christian W. Hoffmann, 
http://www.wsl.ch/staff/christian.hoffmann
Mathematics + Statistical Computing   e-mail: christian.hoffmann at wsl.ch
Swiss Federal Research Institute WSL  Tel: ++41-44-73922-   -77  (office)
CH-8903 Birmensdorf, Switzerland             -11(exchange), -15  (fax)




More information about the R-help mailing list