Skip to content
Prev 24021 / 63424 Next

Functions that write functions in R packages

How about something like this where we put the accessors in
.GlobalEnv at object construction time in this example but you
could alternately place them into package:ggplot or elsewhere on
the search path:

library(proto)

make.accessors <- function(p, e = p, ...)
  lapply(ls(p, ...), function(v) {
     if (is.function(get(v, p))) e[[v]] <- do.call("$.proto", list(p, v))
  invisible(p)
})
p <- proto(x = function(.) 1, y = function(.) 2)
make.accessors(p, .GlobalEnv)
x()
print(x)
y()
print(y)
rm(x, y)

# or the constructor of objects like p could build it right it
# at object construction time
make.p <- function(..., e = .GlobalEnv) make.accessors(proto(...), e = e)
q <- make.p(x = function(.) 1, y = function(.) 2)
x()
print(x)
y()
print(y)
On 2/24/07, hadley wickham <h.wickham at gmail.com> wrote: