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.
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
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 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.
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')
Related Topics
Get the Vector of Values from Different Columns of a Matrix
Plot Negative Values in Logarithmic Scale with Ggplot 2
Adjusting the Width of Legend for Continuous Variable
How to Build a Crossword-Like Plot for a Boolean Matrix
Read.Table Reads "T" as True and "F" as False, How to Avoid
Generating Non-Duplicate Combination Pairs in R
Suppress Automatic Output to Console in R
R: Faceted Bar Chart with Percentages Labels Independent for Each Plot
R Remove Multiple Text Strings in Data Frame
Send a Text String Containing Double Quotes to Function
Ggplot2 One Line Per Each Row Dataframe
Scales = "Free" Works for Facet_Wrap But Doesn't for Facet_Grid
Error: Maximal Number of Dlls Reached
Load a Dataset into R with Data() Using a Variable Instead of the Dataset Name
Specify Position of Geom_Text by Keywords Like "Top", "Bottom", "Left", "Right", "Center"
Package Domc Not Available for R Version 3.0.0 Warning in Install.Packages