You can do that with
   environment(fun) <- new.env(parent=parent.env())
but then your function, function(b)a+b, will not be able
to find the "+" function.

Putting this code into a package simplifies things a lot.

E.g., I added a file containing the two lines
   phi <- sum(1/(1:1e6)) - log(1e6) # 0.5772...
   fun <- function(b) a + b + phi
to a package (and added phi and fun to the NAMESPACE file's export
command).  Then R CMD check myPackage reported
   * checking R code for possible problems ... Note
   fun: no visible binding for global variable 'a'
It did not complain about the "+" or "phi", since they are
either in the package or in some package required by my
package but it did complain about the "a".

Furthermore, at runtime, fun will always get phi from the package,
even if I happen to have a phi in  my global environment.
  > fun(10)
  Error in fun(10) : object 'a' not found
  > a <- 1000
  > fun(10) # uses .GlobalEnv's a
  [1] 1010.577
  > phi <- 100
  > fun(10) # still uses myPackage's phi
  [1] 1010.577

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

From: Gang Peng [mailto:michael.gang.p...@gmail.com]
Sent: Thursday, September 12, 2013 2:54 PM
To: William Dunlap
Cc: Sarah Goslee; r-help@r-project.org
Subject: Re: [R] How to avoid searching variables in global environment

Hi Bill,

Thanks. I think we can define the function in the environment whose parent 
environment is empty environment. But don't know how.
Best,
Mike


2013/9/12 William Dunlap <wdun...@tibco.com<mailto:wdun...@tibco.com>>
If you want to find out if you've forgotten to make 'a' an argument
you can use codetools::findGlobals(func) to list the names that don't
refer to something already defined in the 'func':
  > fun <- function(b) a + b
  > library(codetools)
  > findGlobals(fun)
  [1] "+" "a"
You need to filter out things defined in some attached package (like the "+"
from the base package).  I think that when you run 'R CMD check' on a
package this sort of check is made on the functions in the package.
findGlobals does not run the function to make its checks, it just looks at
the function.

A kludgy way to get an error message instead of having the function silently
use something from .GlobalEnv is to make the environment of the function
the parent of .GlobalEnv
   >  a <- 1000
   > environment(fun) <- parent.env(globalenv())
   > fun(7)
   Error in fun(7) : object 'a' not found
Since this is a run-time check it will not find problems in branches of the
code that are not run.  The parent environment of .GlobalEnv changes
every time you attach or detach a package.  For production code you will
want to use a package - the namespace mechanism and the check command
will take care of most of this sort of problem.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>


> -----Original Message-----
> From: r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org> 
> [mailto:r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org>] On 
> Behalf
> Of Sarah Goslee
> Sent: Thursday, September 12, 2013 2:09 PM
> To: Gang Peng
> Cc: r-help@r-project.org<mailto:r-help@r-project.org>
> Subject: Re: [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.p...@gmail.com<mailto:michael.gang.p...@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
>
> ______________________________________________
> R-help@r-project.org<mailto:R-help@r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to