[R] eval in correct frame?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 25 11:29:23 CET 2004


Christian Hoffmann <christian.hoffmann at wsl.ch> writes:

> 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.

I think you are looking for eval.parent() (in both cases; try using
prinside on something called "txt")

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list