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:
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
"no visible binding for global variable" and with() vs. within()
5 messages · Daniel Braithwaite, Duncan Murdoch, Rolf Turner +2 more
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(). Duncan Murdoch
R version 3.1.1 (2014-07-10) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base [[alternative HTML version deleted]]
______________________________________________ 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.
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:
utils::globalVariables("x")
cheers,
Rolf
Rolf Turner Technical Editor ANZJS
4 days later
Rolf Turner <r.turner at auckland.ac.nz>
on Mon, 18 Aug 2014 08:47:36 +1200 writes:
> 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:
> utils::globalVariables("x")
Yes, but that would be a quite bad idea, IMHO:
The checking code {from package 'codetools' BTW}
would no longer warn you about any accidental global 'x'
variable in any of your functions in your package.
After all, these codetools checks *are* very helpful in
detecting typos and thinkos.
Consequently, I'd strongly advise to only use
globalVariables(.) on *rare* variable names.
Martin Maechler,
ETH Zurich
I'm dealing with these type of false NOTEs as:
foo1 <- function (bar) {
# To please R CMD check
x <- NULL; rm(list="x")
with(bar, {
x })
}
Of course, that may one day break with more clever code inspections.
My $.02
/Henrik
On Fri, Aug 22, 2014 at 2:40 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
Rolf Turner <r.turner at auckland.ac.nz>
on Mon, 18 Aug 2014 08:47:36 +1200 writes:
> 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:
> utils::globalVariables("x")
Yes, but that would be a quite bad idea, IMHO:
The checking code {from package 'codetools' BTW}
would no longer warn you about any accidental global 'x'
variable in any of your functions in your package.
After all, these codetools checks *are* very helpful in
detecting typos and thinkos.
Consequently, I'd strongly advise to only use
globalVariables(.) on *rare* variable names.
Martin Maechler,
ETH Zurich
______________________________________________ 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.