[R] printing a generated function crashes R

Mstislav Elagin elagin at wias-berlin.de
Wed Sep 13 16:15:57 CEST 2006


Dear All,

the last expression in the following code snippet crashes R (version 
2.3.1 on Windows XP) when run interactively:

make.bad.function <- function(kind)
{
   zz <- switch(kind,
                "1" = 1,
                "2" = 2)

   stopifnot( !is.null(zz) )

   eval( bquote( function(x)
                {
                  x + .(zz)
                }))
}

# bad.function <- make.bad.function("5") ## error as expected

bad.function <- make.bad.function("1")
print(bad.function(10)) ## -> 11

bad.function <- make.bad.function("2")
print(bad.function(10)) ## -> 12

bad.function            ## this works if the code is source()'d
print(bad.function)     ## oops!

However, it does work (i.e. prints the body of bad.function) if run 
non-interactively
(R --vanilla < bad-function.R).

Any ideas why this happens?

Thanks in advance

Mstislav Elagin



More information about the R-help mailing list