[R] on.exit processing

Luke Tierney luke at nokomis.stat.umn.edu
Mon Sep 10 17:11:43 CEST 2001


Yuk!

I think the problem is in the way on.exit actions are installed.
Looking at the output of debug it looks like we are doing the add by
destructively modifying the expression, which in turn modifies the
function code itself:

> f(0)
first exit call
> f(1)
first exit call
second exit call
> f(1)
first exit call
second exit call
second exit call
> debug(f)
> f(1)
debugging in: f(1)
debug: {
    on.exit({
        cat("first exit call\n")
        {
            cat("second exit call\n")
        }
        {
            cat("second exit call\n")
        }
    })
    if (x == 1) 
        on.exit({
            cat("second exit call\n")
        }, add = T)
    invisible(NULL)
}
Browse[1]> 
debug: on.exit({
    cat("first exit call\n")
    {
        cat("second exit call\n")
    }
    {
        cat("second exit call\n")
    }
})

Looking at the code it would appear that this only happens when the
existing on.exit form has braces around it. Looks like the code


		ctxt->conexit = listAppend(oldcode,tmp);

in builtin.c:do_onexit should be something like

		ctxt->conexit = listAppend(duplicate(oldcode),tmp);

but this needs a bit more checking.

luke


On Mon, Sep 10, 2001 at 11:01:39AM -0400, james.holtman at convergys.com wrote:
> I have encountered a 'strange' behavior in the 'on.exit' processing.  I had
> a function where I setup an 'on.exit' condition and then later on added to
> it with an 'on.exit({...}, add=T)'.  What appeared to happen is that on
> subsequent calls to the function, even if only the first 'on.exit' was
> called, it was still executing the one with the 'add=T'.
> 
> I am using Version 1.3.0  (2001-06-22) on Windows 2000.
> 
> Here is a small sample of what is happening.  The function conditionally
> sets up the on.exit, and as you can see, each time the 'second' one is
> setup, it just keeps adding to what is output:
> 
> 
> > x.func <- function(x=0){
> +     on.exit({cat('first exit call\n')})
> +     if (x == 1) on.exit({cat('second exit call\n')}, add=T)
> +     invisible(NULL)
> + }
> > x.func(0)   # only the 'first' exit
> first exit call
> > x.func(1)   # both 'first' & 'second'
> first exit call
> second exit call
> > x.func(0)   # only the 'first'
> first exit call
> second exit call         #<<  shouldn't be here
> > x.func(1)   # both 'first' & 'second'
> first exit call
> second exit call
> second exit call         #<<  shouldn't be here
> > x.func(1)   # both 'first' & 'second'
> first exit call
> second exit call
> second exit call         #<<  shouldn't be here
> second exit call         #<<  shouldn't be here
> > x.func(1)   # both 'first' & 'second'
> first exit call
> second exit call
> second exit call         #<<  shouldn't be here
> second exit call         #<<  shouldn't be here
> second exit call         #<<  shouldn't be here
> >
> 
> --
> 
> NOTICE:  The information contained in this electronic mail transmission is
> intended by Convergys Corporation for the use of the named individual or
> entity to which it is directed and may contain information that is
> privileged or otherwise confidential.  If you have received this electronic
> mail transmission in error, please delete it from your system without
> copying or forwarding it, and notify the sender of the error by reply email
> or by telephone (collect), so that the sender's address records can be
> corrected.
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Luke Tierney
University of Minnesota                      Phone:           612-625-7843
School of Statistics                         Fax:             612-624-8868
313 Ford Hall, 224 Church St. S.E.           email:      luke at stat.umn.edu
Minneapolis, MN 55455 USA                    WWW:  http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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