From ripley at stats.ox.ac.uk  Fri Dec  2 12:37:37 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 2 Dec 2005 11:37:37 +0000 (GMT)
Subject: [Rd] Enlightenment sought and a possible buglet in vector.Rd
In-Reply-To: <17296.8817.937646.30624@bossiaea.maths.uwa.edu.au>
References: <17296.8817.937646.30624@bossiaea.maths.uwa.edu.au>
Message-ID: <Pine.LNX.4.61.0512021120450.25330@gannet.stats>

On Fri, 2 Dec 2005, Berwin A Turlach wrote:

> Secondly, I stumbled across two behaviours of R that I cannot explain
> but would like to know why R behaves as it does.  But since I expect
> the explanations to be quite technical, I though that r-devel is the
> more appropriate list to ask on than r-help.
>
> The first example is the following:
>
>       > f1
>       function(){
>           par.def <- par(no.readonly=TRUE)
>           on.exit(par(par.def))
>           tt <- sys.on.exit()
>           print(tt)
>           str(tt)
>           invisible()
>         }
>       > f1()
>       par(par.def)
>        language par(par.def)
>       > f2
>       function(){
>           par.def <- par(no.readonly=TRUE)
>           on.exit(par(par.def))
>           print(tt <- sys.on.exit())
>           str(tt)
>           invisible()
>         }
>       > f2()
>       NULL
>        NULL
>
> I found in the R language definition manual the passage that
> discourages users of assigning objects within function calls since it
> is not guaranteed that the assignment is ever made because of R's lazy
> evaluation model.  But this does not seem to explain the above
> behaviour since the argument to print is evaluated.  If I replace
> sys.on.exit() with, say, ls() in both functions, then they produce the
> same output (and the output that I expect).  Why does f2() not work
> with sys.on.exit()?

It does work, but you seems to have misunderstood what it does.  See e.g. 
the `bug' report discussed at

 	http://tolstoy.newcastle.edu.au/~rking/R/devel/05/02/2112.html

You might find

g <- function(test) {}
f2 <- function(){
        par.def <- par(no.readonly=TRUE)
        on.exit(par(par.def))
        g(tt <- sys.on.exit())
        str(tt)
        invisible()
}
f2()

illuminating.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Enlightenment sought and a possible buglet in vector.Rd
Prof Brian Ripley