[R] question from Blue book

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 3 10:03:46 CET 2008


On Thu, 3 Jan 2008, hodgess wrote:

>
>    Hi R people:
>
> On page 235 of the Blue Book (Becker, Chambers, and Wilks), there is a "compose2" function.
>
> compose2 <- function(f,g) {
> gg <- substitute(g1(x))
> print(gg)
> gg[[1]] <- g
> ff <- substitute(f1(y))
> print(ff)
> ff[[1]] <- f
> ff[[2]] <- gg
> print(ff)
> fun <- function(x){ NULL }
> fun[[2]] <- ff
> fun
> }
>
> However, when I run this function using sum and log (as in the example), I get the following error:
>
> Error in fun[[2]] <- ff : object of type 'closure' is not subsettable
>>
> Has anyone else run into this, please?
> Or is this a merely an issue with a dated concept, please?

The implementation of functions is different in S and in R.  You need to 
use body() and formals() in R (functions which did not exist in S).

>From memory, an n-arg function in S has n+1 components, the arguments plus 
the body.

> Thanks in advance,
> Sincerely,
> Erin Hodgess

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




More information about the R-help mailing list