From murdoch.duncan at gmail.com  Thu Mar 17 10:08:54 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Thu, 17 Mar 2011 05:08:54 -0400
Subject: [Rd] Detecting bad lexical scoping
In-Reply-To: <AANLkTimpAMDo383a2D85OBg4JOhkxzFpyThT4cvt1-7i@mail.gmail.com>
References: <AANLkTimpAMDo383a2D85OBg4JOhkxzFpyThT4cvt1-7i@mail.gmail.com>
Message-ID: <4D81CFA6.9020608@gmail.com>

On 16/03/2011 4:14 PM, Ryan King wrote:
> I've recently hunted down a troublesome bug in my own code, and am
> looking for an easy mechanism to detect this kind of error in other R
> code.  The problem was an undefined variable inside of a function.
> Unfortunately, R looked for that variable in the global environment
> and found it since there was variable with that name in my testing
> scripts (note to self: do not name things "x").
>
> Is there an easy way to report all the non-local objects accessed in a
> function?

The problem is that all base functions are non-local objects, so you 
probably don't want to do exactly that, but the codetools package can do 
it (using tools::findGlobals) and also provides help in identifying 
errors.  You say you keep your code in a package (which is a good idea) 
so you should run "R CMD check" on the package.  If your code is not in 
a package, use tools::checkUsage or related functions.

>
> Is there any easy way around the usual scoping rules?  I want to be
> able to get to base functions, and am willing to namespace:: or :::
> access all of my own functions (it's in a package) if necessary to
> block the standard scoping rules.  The language def section on
> environments made me hurt.

Quoting Uwe Ligges, "No".

Duncan Murdoch



[Rd] Detecting bad lexical scoping
Duncan Murdoch