magrittr icon indicating copy to clipboard operation
magrittr copied to clipboard

R CMD check and no visible binding for global variable '.'

Open gaborcsardi opened this issue 11 years ago • 11 comments

This is more of a question than an issue. You are probably aware of R CMD check complaining about the dot. I could just ignore the complaints of course, but they sometimes signal real errors, so every time I see the NOTE in the output, I need to check.

I just put this in the package:

. <- "Shut up"

Do you see any potential problems with this? It seems to me that it will not interfere with the pipes, but I don't know much about magrittr internals.

gaborcsardi avatar Aug 05 '14 14:08 gaborcsardi

This is fine; but still I get annoyed somtimes..

I just toyed around with something; but it may be too weird. Essentially, I tried bundling the code in this gist (https://gist.github.com/smbache/a528d77f136a4bbf0c19) in a package to see if the compiler would complain. It didn't; and one of the functions will work at runtime (test), the other won't (test2). What I like about it is that you get explicit declaration of a variable which is only intended for nonstandard use; if it used in any standard way, an informative error is thrown. So test is an example of how to use it, and test2 shows the error in action. But maybe the syntax is too weird. Also, I am not sure where the declare function should go as magrittr is not the only package which allows for some cool nonstandard stuff !

smbache avatar Aug 05 '14 18:08 smbache

Cool. declare is fine in magrittr, I guess, people can copy it if they want to. What I am not sure about, however, where the magrittr::declare( . <- as_nonstandard()) should go. It would need to go in each package that uses magrittr, as I see. Or people would need to import the dot. (But FIXME.)

Btw, did you try this with R CMD check? If it happens that it evaluates the promise, then our discussion is pretty academic....

gaborcsardi avatar Aug 05 '14 18:08 gaborcsardi

R CMD is fine with it; I'm not convinced yet that I am ;-)

The <- tricks the check, but is never carried out. Rather delayedAssign does the assignment...

smbache avatar Aug 05 '14 19:08 smbache

Is this entirely a joke?

. <- "Shut up"

I'd really like to make the NOTEs go away….

jennybc avatar Feb 13 '15 19:02 jennybc

@jennybc

Then just put this in the package and the NOTE goes away. You can change the exact text according to your mood. :)

gaborcsardi avatar Feb 13 '15 19:02 gaborcsardi

Or do the official globalVariables(".")

hadley avatar Feb 13 '15 19:02 hadley

That just seems more cumbersome: :)

if(getRversion() >= "2.15.1")  utils::globalVariables(c("."))

More seriously, it is indeed better, it just instructs the check tool to ignore the dot, instead of actually creating a global variable.

gaborcsardi avatar Feb 13 '15 19:02 gaborcsardi

The more you know! Thanks.

jennybc avatar Feb 13 '15 21:02 jennybc

Wondering if the globalVariables should be the "offcial" (e.g. vignette/manual) suggested approach? Another option would be to wrap it in a more expressive magrittr function, which could be called when using magrittr in packages, e.g. something like

declare_dot <- function(add = TRUE) utils::globalVariables(".", add = add)

smbache avatar Feb 16 '15 12:02 smbache

utils::globalVariables(c(".", "%>%")) works well for me in a package which uses magrittr for tests and data pre-processing, but not in user-facing functions: this means I can suggest instead of import magrittr without check complaining.

jackwasey avatar Apr 10 '15 18:04 jackwasey

@gaborcsardi @jennybc I added a formula feature in this branch: https://github.com/smbache/magrittr/tree/simplified

You could then do

letters %>% 
  ~gsub("[a-k]", "z", .)

Small thing, but would make the checker happier..

smbache avatar Feb 03 '16 06:02 smbache