Skip to content

Define "local" function

7 messages · Fernando Saldanha, Roger D. Peng, Gabor Grothendieck +3 more

#
I discovered a bug in a program I am writing which was due to the
program using a global variable within a function.

For example,

myfunc <- function(x) { y}

That is, I made a mistake when defining the function and wrote "y"
when I should have written "x".

However, there was a variable y in the global environment and the
function "worked" but gave the wrong answer.

I would like to avoid this problem by defining a "local" function.
That would mean a function that only accepts as variables those that
were defined within its body or were passed as parameters, and would
generate an error when I try to define it if I am using an "external"
variable. Something like:
Error: using external variable
 
I read the documentation about environments (I still do not understand
a lot of it, have been working with R for four days now), and searched
the newsgroups, but I could not find the way to do this.

Thanks for any suggestions.

FS
#
This came up just a few days ago on the mailing list!  Check the 
archives here:

https://stat.ethz.ch/pipermail/r-help/2005-April/067639.html

-roger
Fernando Saldanha wrote:

  
    
#
On 4/15/05, Fernando Saldanha <fsaldan1 at gmail.com> wrote:
Try this:
Error in f(1) : Object "y" not found
#
I am also very interested how this could be done, possibly in such a way 
that this would be incorporated in the function itself and there wouldn't be 
a need to write "environment(f) <- NULL" before calling a function, as is 
proposed in the reply below and in a thread a few days ago!

Thanks for any suggestions,
Ales Ziberna

----- Original Message ----- 
From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
To: <fsaldanha at alum.mit.edu>
Cc: "Submissions to R help" <r-help at stat.math.ethz.ch>
Sent: Friday, April 15, 2005 5:05 PM
Subject: Re: [R] Define "local" function
#
You can embed your function in another:

f <- function(x) { f <- function(x) y; environment(f) <- NULL; f(x) }
On 4/15/05, Ales Ziberna <ales.ziberna at guest.arnes.si> wrote:
#
Ales Ziberna <ales.ziberna at guest.arnes.si> writes:
Notice BTW, that environment(f) <- NULL may have unexpected
consequences. What it really means is that the lexical scope of f
becomes the base package. This interpretation of NULL may change in
the future, since it is somewhat illogical and it has a couple of
undesirable consequences that there's no way to specify a truly empty
environment. So

a) if you're calling a function outside of the base package, you get
the effect of
Error in mean(rnorm(10)) : couldn't find function "rnorm"

b) even if it does work now, it may be broken by a future change to R.
Notice that *all* functions contain unbound variables in the form of
functions so if we get an empty NULL environment, even "<-" may stop
working.
#
Peter Dalgaard wrote:
Why worry about where the line occurs?  In R, there is little 
distinction between functions and data, so you could say that the 
function definition isn't done until you're finished modifying it.
A more likely thing to want to do is to see the search path only after 
the global environment, which you can do with

environment(f) <- as.environment(2)

(the 2 says to use the 2nd environment on the search path).  This might 
not be perfect, since you can use attach() to modify the search path, 
but it would likely be good enough to flush out simple programming bugs.

Duncan Murdoch