Skip to content

eval in correct frame?

2 messages · Christian Schulz, Peter Dalgaard

#
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
#
Christian Hoffmann <christian.hoffmann at wsl.ch> writes:
I think you are looking for eval.parent() (in both cases; try using
prinside on something called "txt")