Gang Peng
2013-Sep-12 19:56 UTC
[R] How to avoid searching variables in global environment
For example: a <- 1 f <- function(b){ return(a+b) } when we call function f(2), r will search the local environment first, if it cannot find a, it will search global environment, and return 3. How to avoid r searching the global environment and return an error when we call this function? Thanks. [[alternative HTML version deleted]]
Sarah Goslee
2013-Sep-12 21:08 UTC
[R] How to avoid searching variables in global environment
Hi, You need to specify that a is an argument to the function: On Thu, Sep 12, 2013 at 3:56 PM, Gang Peng <michael.gang.peng at gmail.com> wrote:> For example: > > a <- 1 > > f <- function(b){ > return(a+b) > } >f <- function(b, a) { return(a+b) }> when we call function f(2), r will search the local environment first, if > it cannot find a, it will search global environment, and return 3. How to > avoid r searching the global environment and return an error when we call > this function?The function will now give an error if a is not specified. Sarah -- Sarah Goslee http://www.functionaldiversity.org