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