Re: [R] printing a generated function crashes R

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Wed 13 Sep 2006 - 16:03:45 GMT

Mstislav Elagin <elagin@wias-berlin.de> writes:

> 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?

Well, bquote seems to be doing nasty things if passed an expression with a function inside:

> f <- bquote(function(x) {

+     x + 1
+ }
+ )

> f

function(x) {

    x + 1
}
> eval(f)


H~

H~

Program received signal SIGSEGV, Segmentation fault.

I think the story is that the source attribute is getting messed up.

> z <- eval(f)
> attr(z,"source")

"function(x) {"("x+1}")
> z

X~
X~
..poof..

-- 
   O__  ---- Peter Dalgaard             ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu Sep 14 02:10:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 19 Sep 2006 - 08:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.