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.
Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jan 2008 - 18:30:11 GMT.
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.