[R] "no visible binding for global variable" and with() vs. within()

Rolf Turner r.turner at auckland.ac.nz
Sun Aug 17 22:47:36 CEST 2014

On 17/08/14 23:05, Duncan Murdoch wrote:
> On 16/08/2014, 9:36 PM, Daniel Braithwaite wrote:
>> R CMD check does not object to this code when checking a package:
>> foo1 <- function (bar) {
>>      with(bar, {
>>          x })
>> }
>> but produces a warning:
>> foo2: no visible binding for global variable 'x'
>> in response to this:
>> foo2 <- function (bar) {
>>      within(bar, {
>>          x })
>> }
>> Is this an R bug, or at least, an inadvertent inconsistency?  Here is
>> sessionInfo() from my machine, right after starting an interactive session:
> I'm not sure, but I suspect it's an intentional inconsistency.  The code
> that checks for use of globals can't do anything in with() or within()
> code, so bugs can slip by if you use those.  I think with() had been
> around for a long time and was in wide use when that test was added, but
> within() is newer, and it was less disruptive to warn about it, so the
> warning has been left in.  (I don't remember whether the test came
> before or after within() was introduced.)
> So if you want to avoid the warning, don't use within().

Or you could have a file, say "melvin.R", in the R directory of your
package, containing the line:




Rolf Turner
Technical Editor ANZJS

More information about the R-help mailing list