[R] on.exit

Duncan Murdoch murdoch at stats.uwo.ca
Thu Sep 27 20:30:24 CEST 2001

On Thu, 27 Sep 2001 13:05:01 -0400, you wrote in message
<3BB35C3C.C4FE9451 at umsanet.edu.bo>:

>but what should be better, doesnt work:
>> test1 <- function() {
>+    oldwarn <- options("warn")
>+    options(warn=-1)
>+    on.exit( options(warn=oldwarn))
>+    log(-1)
>+ }
>> test1()
>Error in options(...) : warn parameter invalid
>What is wrong? (I have tried other versions, without success)

The problem is that you're assuming options() behaves the way par()
does, i.e. that options("warn") returns a number.  It doesn't, it
returns a list.  

The simplest way to get what you want is probably

test1 <- function() {
    oldwarn <- options(warn=-1)   # first call
    on.exit( options(oldwarn))       # second call

Notice that the first call that changes the option returns the old
value, so you don't need two calls at the start; the second call that
restores it just uses the list (which has the name of the option
embedded in it).

Duncan Murdoch
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list