How can I handle R CMD check no visible binding for global variable notes when my ggplot2 syntax is sensible?
Have you tried with aes_string
instead of aes
? This should work, although I haven't tried it:
aes_string(x = 'x.values', y = 'y.values')
No visible binding for global variable Note in R CMD check
To get it past R CMD check
you can either :
- Use
get("b")
(but that is onerous) - Place
a=b=NULL
somewhere higher up in your function (that's what I do)
There was a thread on r-devel a while ago where somebody from r-core basically said (from memory) "NOTES are ok, you know. The assumption is that the author checked it and is ok with the NOTE.". But, I agree with you. I do prefer to have CRAN checks return a clean "OK" on all platforms. That way the user is left in no doubt that it passes checks ok.
EDIT :
Here is the r-devel thread I was remembering (from April 2010). So that appears to suggest that there are some situations where there is no known way to avoid the NOTE, but that's ok.
R CMD check: no visible binding for global variable (when using a data/ dataset in the package)
Usually, to get rid of that Note you just have to add a reference like this:
ggplot2::diamonds
R CMD check NOTE: `No visible binding for global variable`, when defining a variable in an S3 generic and using it in a method
Answering myself, I think this practice can lead to confusing behaviour since UseMethod()
generates a call of the appropriate method with its same arguments; implying that modifications to a variable which is an argument of the generic (not like greeting
in my example above) are not propagated to the method.
Right now I'm defining extra functions to be called at the beginning of each method to perform the complex computation I was originally doing in the generic. This can also lead to cumbersome code if these functions require many arguments, but is the best solution I have found so far.
I will leave this open for some time to see if someone has other approaches to propose.
In R, is it possible to suppress Note: no visible binding for global variable?
All credit to @Tyler Rinker for this answer.
To eliminate these notes, prefix the source code above with this:
# Intent:
# This function suppresses the following notes generated by "R CMD check":
# - "Note: no visible binding for global variable '.->ConfigString'"
# - "Note: no visible binding for '<<-' assignment to 'ConfigString'"
# Usage:
# Add the following right in the beginning of the .r file (before the Reference
# class is defined in the sourced .r file):
# suppressBindingNotes(c(".->ConfigString","ConfigString"))
suppressBindingNotes <- function(variablesMentionedInNotes) {
for(variable in variablesMentionedInNotes) {
assign(variable,NULL, envir = .GlobalEnv)
}
}
suppressBindingNotes(c(".->ConfigString","ConfigString"))
In addition, sometimes Revolution R might need to be restarted if it has been running for a long time.
R no visible binding for global variable note when creating variables in sub-routines and returning to environment
SOLUTION USING ENVIRONMENTS
So I figured out how to do this. Yes, you can use the list approach, but it is somewhat artificial. Here is the proper way: define a named empty environment inside the wrapper function outside_function, to which all objects that you want to store (and return at the end) are written. This environment is then passed as a single argument (like a list) to the inside functions. Within inside_function, you can edit stored environment objects in real time, without having to explicitly return the objects in a list back to a list object. It is cleaner.
outside_function <- function() {
myenv <- new.env(parent = emptyenv())
#object k exists in local environment, but not myenv
k <- LETTERS[17:23]
#assign list of objects to
print(ls()) #two objects, k and myenv
print(ls(myenv))
print("first run")
inside_function(env=myenv)
print("LS")
print(as.list(myenv))
print("second run")
inside_function(env=myenv)
print("LS")
print(as.list(myenv))
#inside here, have to refer to objects as list elements
#the command print(m) searches through environments to find an object
#if nothing exists locally, m will find myenv$m, but is misleading
#try(print(m))
#now create a local object m that is different
m <- "blah"
print(m) #gives 'blah'
print(myenv$m)
#return at end as a list
invisible(as.list(myenv))
}
inside_function <- function(env) {
#create/overwrite objects in env
env$m <- matrix(stats::runif(4), ncol=2)
#these are created in real time within inside_function without having
#to return env (notice NULL is a returned value)
print(env$m)
#overwite
env$m <- matrix(stats::runif(4), ncol=2)
print(env$m)
env$d <- 5
print(env$d)
env$d <- env$d + runif(1)
env$z <- letters[sample(1:20, size=6)]
invisible(NULL)
}
tmp <- outside_function()
print(tmp) #contains all the objects as a list
No visible global function definition/no visible binding for global variable in a project?
You could disable the object usage linter in your project. Example: https://github.com/ropensci/targets/blob/8c42336a228874889c9ee99dd154c567b4eee91a/.lintr#L4. More on linters: https://github.com/r-lib/lintr#available-linters
Related Topics
Pasting Two Vectors With Combinations of All Vectors' Elements
How to Tell What Is in One Vector and Not Another
Subscript Letters in Ggplot Axis Label
Wrap Long Axis Labels Via Labeller=Label_Wrap in Ggplot2
Splitting a Continuous Variable into Equal Sized Groups
Custom Legend For Multiple Layer Ggplot
Convert Unix Epoch to Date Object
Creating a Comma Separated Vector
Sample N Random Rows Per Group in a Dataframe
How to Use Grep()/Gsub() to Find Exact Match
How to Put Labels Over Geom_Bar in R With Ggplot2
Replace Multiple Letters With Accents With Gsub
How to Replace Na Values in a Table For Selected Columns
Basic Lag in R Vector/Dataframe
Dplyr Summarise: Equivalent of ".Drop=False" to Keep Groups With Zero Length in Output