Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use utils::globalVariables

Following your recommendations (or trying to do it, at least), I have tried some options, but the problem remains, so there must be something I am missing.

I have included a more complete code

setwd("C:/naapp")
#' @import utils 
#' @import devtools 

I have tried with and without using suppressForeignCheck

if(getRversion() >= "2.15.1"){

  utils::globalVariables(c("eleven"))
  utils::suppressForeignCheck(c("eleven"))

  }


myFunctionSum <- function(X){print(X+eleven)}

myFunctionMul <- function(X){print(X*eleven)}

myFunction11 <- function(X){
  assign("eleven",11,envir=environment(myFunctionMul))
  }

maybe I should use a particular environment?

package.skeleton(name = "myPack11", list=ls(),
                 path = "C:/naapp", force = TRUE,
                 code_files = character())

I remove the "man" directory from the directory myPack11, otherwise I would get an error because the help files are empty. I add the imports utils, and devtools to the descrption

Then I run check

devtools::check("myPack11")

And I still get this note

#checking R code for possible problems ... NOTE
#myFunctionMul: no visible binding for global variable 'eleven'
#myFunctionSum: no visible binding for global variable 'eleven'
#Undefined global functions or variables:eleven

I have tried also to make an enviroment, combining Tomas Kalibera's suggetion and an example I found in the Internet.

myEnvir <- new.env()
myEnvir$eleven <- 11

etc In this case, I get the same note, but with "myEnvir", instead of "eleven"


First version of the question

I trying to use "globalVariables" from the package utils. I am building an interface in R and I am planning to submit to CRAN. This is my first time, so, sorry if the question is very basic.I have read the help and I have tried to find examples, but I still don't know how to use it.

I have made a little silly example to ilustrate my question, which is: Where do I have to place this line exactly?:

if(getRversion() >= "2.15.1"){utils::globalVariables("eleven")}

My example has three functions. myFunction11 creates the global variable "eleven" and the other two functions manipulate it. In my real code, I cannot use arguments in the functions that are called by means of a button. Consider that this is just a silly example to learn how to use globalVariables (to avoid binding notes).

myFunction11 <- function(){

  assign("eleven",11,envir=environment(myFunctionSum))

}

myFunctionSum <- function(X){

  print(X+eleven)

}

myFunctionMul <- function(X){

  print(X*eleven)

}

Thank you in advance

like image 324
Marina Avatar asked May 09 '26 21:05

Marina


1 Answers

I thought that the file globals.R would be automatically generated when using globalsVariables. The problem was that I needed to create the package skeleton, then create the file globals.R, add it to the R directory in the package and check the package.

So, I needed to place this in a different file:

#' @import utils 
utils::globalVariables(c("eleven"))

and save it

like image 94
Marina Avatar answered May 12 '26 13:05

Marina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!