magrittr
magrittr copied to clipboard
R CMD check and no visible binding for global variable '.'
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.
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 !
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....
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...
Is this entirely a joke?
. <- "Shut up"
I'd really like to make the NOTEs go away….
@jennybc
Then just put this in the package and the NOTE goes away. You can change the exact text according to your mood. :)
Or do the official globalVariables(".")
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.
The more you know! Thanks.
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)
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.
@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..