[Rd] experiments with slot functions and possible problems NOTE

Thomas Petzoldt Thomas.Petzoldt at tu-dresden.de
Mon Jan 21 17:50:24 CET 2008


Duncan Murdoch wrote:
> On 1/21/2008 9:58 AM, Thomas Petzoldt wrote:
>> Hello Duncan,
>>
>> thank you very much for your prompt reply. When I interpret your answer
>> correctly there seems to be no alternative than either:
>>
>> A) using lots of (possibly private) functions in the package or,
>> B) define dummies for all functions which are in such lists or,
>> C) ignore the NOTE, knowing that it is spurious (BTW: there are several
>> prominent packages on CRAN with unresolved NOTEs).

[...]

> There's another way, which is more R-like, if you really want to avoid
> lots of private functions.  That is to create your lists via a function,
> and define the functions in the lists locally within the creator.  That
> is, something like this:
>
> MakeListA <- function() {
>   foo <- function() {
>       1:10
>   }
>   bar <-function() {
>        log(foo())
>   }
>   return(list(foo = foo, bar = bar))
> }
>
> fListA <- MakeListA()
>
> This avoids the explicit environment manipulations.  Because both foo
> and bar are defined locally within MakeListA, they share an environment
> there, and can see each other (and anything else you chose to define
> locally within MakeListA.)

[...]

Cool! What about the following (AFAIK getting environments is "legal" as
opposed to setting them):

MakeListA <- function() {
    foo <- function() {
      1:10
    }
    bar <-function() {
      log(foo())
    }
    return(as.list(environment()))
}

fListA <- MakeListA()

makefun <- function(flist) {
   with(flist,
     function() bar() + foo()
   )
}
toplevel <- makefun(fListA)
toplevel()

## but it is not possible to naively replace functions afterwards:
fListA$bar <- function() cos(foo())
toplevel <- makefun(fListA)
toplevel()

## Error in bar() : could not find function "foo"


Note that it's the same in the "explicit" and in the environment version.

-- Thomas P.



More information about the R-devel mailing list