Skip to content
Back to formatted view

Raw Message

Message-ID: <425FD841.1060806@jhsph.edu>
Date: 2005-04-15T15:05:37Z
From: Roger D. Peng
Subject: Define "local" function
In-Reply-To: <10dee46905041507592bdd588a@mail.gmail.com>

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:
> 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:
> 
> 
>>myfunc <- function(x, type = 'local') { y}
> 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/