Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using compiler- package and suppress "No visible binding for global variable"

Tags:

package

r

I'm using R 3.0.2 on Ubuntu 14. I make some heavy calculations in my code, and I tried out the "compiler" package with

compilePKGS(enable=TRUE)
enableJIT(3)

And it seems to speed up my code. Very nice!

But everytime my package enables the "compiler", I get a lot of notes like

Note: no visible binding for global variable '.Data'

or something similar with my own S4 objects (its "obj@result" in the code):

Note: no visible binding for global variable 'result'

which is, for example, part of a self-made S4 object. Adding setCompilerOptions("suppressAll", TRUE) or setCompilerOptions("suppressUndefined", TRUE)didn't help. When I deactivate the compiler package completely, no notes pop up at all, so this might be a problem with my understanding of the compiler-package/jit?

What can I do to suppress these notes?

Edit:

require(compiler)
compilePKGS(enable=TRUE)
enableJIT(3)



setClass(Class = "testobject", 
         slots = c( data     = "numeric",   
                    test     = "character", 
                    split    = "numeric",   
                    name     = "character"  
         )
)

a <- new("testobject", data=c(1,2,3,4), test="TEST", split=5, name="NAME")

for(i in a@data){
  print(i)
}

Simple example produces

Note: no visible binding for global variable '.Data' 
Note: no visible binding for global variable '.Data' 

directly after the ClassDefinition Call

like image 831
Marc Avatar asked Jun 17 '14 08:06

Marc


People also ask

Is there any visible binding for global variable ‘?

no visible binding for global variable ‘.’ This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Does my_FN have any binding for the global variable hp?

NOTE my_fn: no visible binding for global variable ‘mpg_div_hp’ my_fn: no visible binding for global variable ‘mpg’ my_fn: no visible binding for global variable ‘hp’ Undefined global functions or variables: hp mpg mpg_div_hp

How to define unbound variables in a package?

Any package wide unbound variables, which are typically syntactic sugar (e.g. := ), should be defined within the package description file inside a globalVariables () function, which should be a very short and maintainable list. To leave a comment for the author, please follow the link and comment on their blog: Random R Ramblings.

How to avoid undefined global variables when programming with dplyr?

When programming with dplyr, what is the correct way to avoid undefined global variables? You can put the following R code somewhere in your package, perhaps an R/globals.R script. I see this has been solved but here is a dplyr / rlang specific suggestion: you can use the .data pronoun cf this dplyr vignette,


1 Answers

You can suppress these notes from R using

setCompilerOptions(suppressAll = TRUE)

There is no need to separately suppress the "undefined" options, suppressing "all" will do. Alternatively, you can set up environment variable

export R_COMPILER_SUPPRESS_ALL=true (or similarly under different OSes).

If you wanted to suppress only warnings about variables that are or seem to be undefined to the compiler, you can do

setCompilerOptions(suppressUndefined=TRUE)

And if you wanted to do this only for variable .Data, you can do

setCompilerOptions(suppressUndefined=".Data").

Note also there is no need to enable compilation of packages when you use compiler to speed up your code, all you need to do is enable the JIT. You can do it from R as in your example, or just setting another variable

export R_ENABLE_JIT=3

To enable most aggressive optimizations, you can also set up

export R_COMPILER_OPTIMIZE=3

or from R run setCompilerOptions(optimize=3)

When enabling JIT compilation via environment variable, you don't have to explicitly load the compiler package - it will be done automatically.

like image 147
Tomas Kalibera Avatar answered Sep 20 '22 13:09

Tomas Kalibera