From murdoch.duncan at gmail.com  Sun Apr 10 03:12:48 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sat, 09 Apr 2011 21:12:48 -0400
Subject: [Rd] Wish there were a "strict mode" for R interpreter. What
 about You?
In-Reply-To: <4DA0E592.1040507@prodsyse.com>
References: <BANLkTim=6ae2huv+GYQLMtgs2P+GD8_WtA@mail.gmail.com>	<BANLkTinJO=3Epmq_2zH+3vbGmZgPp_gFGA@mail.gmail.com>
	<4DA0E592.1040507@prodsyse.com>
Message-ID: <4DA10410.2040909@gmail.com>

On 11-04-09 7:02 PM, Spencer Graves wrote:
> On 4/9/2011 2:31 PM, Hadley Wickham wrote:
>> On Sat, Apr 9, 2011 at 2:51 PM, Paul Johnson<pauljohn32 at gmail.com>   wrote:
>>> Years ago, I did lots of Perl programming. Perl will let you be lazy
>>> and write functions that refer to undefined variables (like R does),
>>> but there is also a strict mode so the interpreter will block anything
>>> when a variable is mentioned that has not been defined. I wish there
>>> were a strict mode for checking R functions.
>>>
>>> Here's why. We have a lot of students writing R functions around here
>>> and they run into trouble because they use the same name for things
>>> inside and outside of functions. When they call functions that have
>>> mistaken or undefined references to names that they use elsewhere,
>>> then variables that are in the environment are accidentally used. Know
>>> what I mean?
>>>
>>> dat<- whatever
>>>
>>> someNewFunction<- function(z, w){
>>>     #do something with z and w and create a new "dat"
>>>     # but forget to name it "dat"
>>>      lm (y, x, data=dat)
>>>     # lm just used wrong data
>>> }
>>>
>>> I wish R had a strict mode to return an error in that case. Users
>>> don't realize they are getting nonsense because R finds things to fill
>>> in for their mistakes.
>>>
>>> Is this possible?  Does anybody agree it would be good?
>>
>>> library(codetools)
>>> checkUsage(someNewFunction)
>> <anonymous>: no visible binding for global variable ?y?
>> <anonymous>: no visible binding for global variable ?x?
>> <anonymous>: no visible binding for global variable ?dat?
>>
>> Which also picks up another bug in your function ;)
>
>         Is this run by "R CMD check"?  I've seen this message.
>
>
>         "R CMD check" will give this message sometimes when I don't feel
> it's appropriate.  For example, I define a data object ETB in a package,
> then give that as the default in a function call like
> f(data.=ETB){if(missing(data.))data(ETB);  data.}.  When I run "R CMD
> check", I get "no visible binding for global variable 'ETB'", even
> though the function is tested and works during R CMD check.

What is ETB?  Your code is looking for a global variable by that name, 
and that's what codetools is telling you.

Duncan Murdoch



[Rd] Wish there were a "strict mode" for R interpreter. What about You?
Duncan Murdoch