[R] findGlobals on apply

Christophe Genolini cgenolin at u-paris10.fr
Wed Apr 9 00:00:50 CEST 2008


Hi Luke

Thanks for all these explanation, things are clearer.

Let me go back on my initial problem, that was, as a programmer, I would 
like to have a tool to detect typo by detecting globals variables:
I get that findGlobals is not design for that.

I did not realy understand the use of checkUsage (sorry for that, I am 
not a high level programmer).
But I find an example on which checkUsage does not detect the typo either:

mObjBis <- 3
f <- function(myOb){
myObBis <- myOb^2
plot(myObBis)
return(mObjBis)
}
checkUsage(f,all=TRUE)

So, my question is a more general question : is there a function that 
can detect global variable with exclusion of the function and the 
reserved word?

For example:
g <- function(x)return(x+pi)
we don't want pi to be considere as a global variable since it is a 
constant...

Is there a function that can deal with that ?

Christophe

> 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.
>>
>



More information about the R-help mailing list