[R] programming advice?
Seth Falcon
sfalcon at fhcrc.org
Fri Apr 21 17:09:34 CEST 2006
"Charles Annis, P.E." <Charles.Annis at StatisticalEngineering.com>
writes:
> Dear R-helpers:
>
> I am doing some exploratory programming and am considering a routine that
> has several other routines defined within it, so that I can avoid a large
> and messy global re-programming to avoid naming conflicts.
>
> My question is this: Because it is interpreted, does R have to re-build
> these internal routines every time the new routine is called?
If you mean:
f <- function(x) {
f1 <- function(y) {...}
f2 <- function(y) {...}
f3 <- function(y) {...}
f1(x) + f2(x) + f3(x)
}
Then, yes, as I understand it, each call to f() will include the
overhead of defining functions f1, f2, and f3. In most cases, I would
expect this overhead to be quite small in relation to the actual
computations you are doing. You can probably use Rprof() to confirm
this.
You can also look at local() which I think provides a similar local
namespace, but would not require redefinition of the helper functions.
Here is a small example:
f <- local({
f1 <- function(y) 2*y
f2 <- function(y) y + 10
function(x) {
f1(x) + f2(x)
}
})
+ seth
More information about the R-help
mailing list