In R, How to Suppress "Note: No Visible Binding for Global Variable"

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



Leave a reply



Submit