Skip to content

R CMD check: no visible binding for global variable

4 messages · swaraj basu, Rolf Turner, William Dunlap

#
On 17/05/11 06:05, swaraj basu wrote:
This may be a case of the blind leading the blind, but here goes.

Understanding is simple (I think!). You are referring to an object 
"annotIndex"
inside a function, as in

foo <- function(x) {
x + annotIndex # Don't worry about the fact that this doesn't make sense!
}

When the package checker looks at this it says "Hmmmm, annotIndex has never
been defined (inside "foo") so it might not exist. There *could* be a 
problem here."

What to do about it: One solution is just to ignore the note; it's only 
a note,
not an error, nor even a warning, and you know that there isn't actually
a problem.

The solution that is often suggested --- see

RSiteSearch("{no visible binding}",restrict=c("Rhelp02","Rhelp08"))

of assigning annotIndex a value, e.g. annotIndex <- NULL, at the beginning
of your function doesn't work here since that local NULL value will 
over-ride the
global value that you really want.

You might try (the appropriate analogue of) x + 
get("annotIndex",envir=.GlobalEnv).

That should satisfy the package checker. (I haven't tested it, but.)

Dunno if this has an adverse impact on the efficiency of your code.

HTH

cheers,

Rolf Turner
#
Follow-up to my previous message; just remembered another work-around
for the ``no visible binding'' note.

Wrap up your calls that involve "annotIndex" in text strings and then
evaluate these strings.

E.g.:

     xxx <- "y <- x + annotIndex"
     eval(parse(text=xxx))

That will fool the package checker! :-)

Might be more efficient than my suggestion of using ``get()''.

     cheers,

         Rolf Turner
#
If you install you package does the following work
in a fresh R session
   library(yourPackage)
   annotIndex
or do you need to say data(annotIndex) before using
that object?  (This may depend on whether yourPackage
uses a namespace or not.)  If so then adding the line
   LazyData: yes
to DESCRIPTION should eliminate the need for the call
to data(annotIndex) and make check happy as well.
It works for me with R 2.13.0 on Linux.

I am not an expert on the details of the DESCRIPTION
and NAMESPACE files in R packages and others who know
better will probably chime in.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com