On Sun, Feb 8, 2015 at 8:44 PM, Suharto Anggono Suharto Anggono via R-devel <r-devel@r-project.org> wrote: > Sorry to intervene.
No, I'm very happy you intervened. You're comment is 100% valid/correct making my wish moot. Your explanation is very clear and nails it; one should use eval(substitute(expr)) or evalq(expr) for what I'm trying to do. It all came from me trying to prevent > withOptions({x <- 1}, foo=1) from printed the value, where (somewhat simplified): withOptions <- function(expr, ..., envir=parent.frame()) { oopts <- options(...) on.exit(options(oopts)) eval(expr, envir=envir) } I have a few of these withNnn() functions, but for this particular one (*) I had forgotten an expr <- substitute(expr) in there, which caused me to incorrectly blame eval(). <recursive mistake>This is very much the same problem as you observed with my eval2() example.</recursive mistake> Thank you very much Henrik (*) Actually withSeeds() which is to messy to use as an example. > > Argument passed to 'eval' is evaluated first. > So, > eval(x <- 2) > is effectively like > { x <- 2; eval(2) } , > which is effectively > { x <- 2; 2 } . > The result is visible. > > eval(expression(x <- 2)) > or > eval(quote(x <- 2)) > or > evalq(x <- 2) > gives the same effect as > x <- 2 . > The result is invisible. > > In function 'eval2', > res <- eval(withVisible(expr), envir=envir, ...) > is effectively > res <- withVisible(expr) . > > ------------------- > > Would it be possible to have the value of eval() preserve the > "visibility" of the value of the expression? > > > "PROBLEM": > > # Invisible >> x <- 1 > > # Visible >> eval(x <- 2) > [1] 2 > > "TROUBLESHOOTING": >> withVisible(x <- 1) > $value > [1] 1 > $visible > [1] FALSE > >> withVisible(eval(x <- 2)) > $value > [1] 2 > $visible > [1] TRUE > > > WORKAROUND: > eval2 <- function(expr, envir=parent.frame(), ...) { > res <- eval(withVisible(expr), envir=envir, ...) > value <- res$value > if (res$visible) value else invisible(value) > } > >> x <- 1 >> eval(x <- 2) > [1] 2 >> eval2(x <- 3) >> x > [1] 3 > > /Henrik > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel