On Tue, 8 Apr 2008, Christophe Genolini wrote:
>
>>> f <- function(x){apply(x,2,mean)}
>>> findGlobals(f)
>> mean is a global variable, so findGlobals gets it right.
> That sound strange to me: a "variable" is something that vary...
mean
> does not vary. maen will ge an argument that is a line of x and will
> make some calculous on it, that is the comportement of a function.
> Of course, mean is an argument of an other function, but I do not think
> this is a reason good enouth to say that mean is a variable.
You are missing some points about R and findGlobals.
In R, functions are first class values: they can be assigned to
variables, passed as arguments, and returned as results, just like
vectors. In contrast to many other languages there is not special
mechanism for defining functions and associating them with a name --
the way you define a function is
foo <- function(...) ...
which creates a function value and assigns it to a variable.
findGlobals just looks at the function body and arguments and
determines which of the variables used would have their definitions
looked up in the global environment if this code is run. It does not
try to detect which of these have values or not, never mind what the
types of those falues are.
The result returned by findGlobals with merge=FALSE separates into
variables that are explicitly used as funcitons, i.e foo in foo(x) and
ones that are not. One could argue that findGlobals should know
enough about apply() to realize that the FUN argument is implicitly
used as a function; if this change were made then
apply(x, 2, pi)
pi would be listed as a function because it is _used_ that way.
> Furthemore, I use findGlobals to detect some typo. In
>
> f <- function(myObject){return(mObject^2)}
>
> findGlobals will detect that mObject is a global so I know there is a
> typo somewhere.
> Considering mean as a globals do not let us use findGlobals this way.
You need to do some extra work to get this -- checking which globals
have values, and maybe whether those that are used as functions have
values that are functions. checkUsage does this, among other things.
For this example checkUsage produces
> checkUsage(f)
<anonymous>: no visible binding for global variable ???mObject???
luke
> Christophe
>
> ______________________________________________
> R-help at 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.
>
--
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke at stat.uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu