From hb at biostat.ucsf.edu  Sun Nov  3 05:24:03 2013
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Sat, 2 Nov 2013 21:24:03 -0700
Subject: [Rd] on.exit() & sys.on.exit(): Calling them via eval() does not
	work as hoped
Message-ID: <CAFDcVCTsGqADPyf_MAiNmLKZofLpy-BkXGABXT6G5=kjotMQcw@mail.gmail.com>

Why does the following eval() call on sys.on.exit() not return what I
expect/evaluate in the proper environment?

foo <- function() {
  cat("foo()...\n");
  on.exit( message("exiting") )

  cat("sys.on.exit():\n")
  res <- sys.on.exit()
  print(res)

  cat("eval(sys.on.exit()):\n")
  expr <- quote(sys.on.exit())
  print(expr)
  res <- eval(expr)
  print(res)

  cat("foo()...done\n")
}

> foo()
foo()...
sys.on.exit():
message("exiting")
eval(sys.on.exit()):
sys.on.exit()
NULL
foo()...done
exiting

Similar problems appear when I try to "record" on.exit() expressions
via eval().  It appears that the "primitives" on.exit() and
sys.on.exit() do something rather special.  Is there a solution to
what I'm trying to do?

The reason why I'm doing this in the first place, is that I'm trying
to implement onExit(<expr>, where="replace"), onExit(<expr>,
where="last"), and onExit(<expr>, where="first").

Thanks,

Henrik



[Rd] on.exit() & sys.on.exit(): Calling them via eval() does not work as hoped
Henrik Bengtsson