[R] How to avoid searching variables in global environment

Sarah Goslee sarah.goslee at gmail.com
Thu Sep 12 23:08:59 CEST 2013


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



More information about the R-help mailing list