Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for utils::globalVariables() #89

Closed
MichalLauer opened this issue Oct 11, 2023 · 2 comments
Closed

Support for utils::globalVariables() #89

MichalLauer opened this issue Oct 11, 2023 · 2 comments

Comments

@MichalLauer
Copy link

If a function uses data masking, the function globals::globalsOf(...) throws an error, as expected.

example <- function() {
  mtcars |>
    dplyr::filter(cyl == 4)
}

globals::globalsOf(example, mustExist = T)
#> Error in globalsByName(names, envir = envir, mustExist = mustExist): Identified global objects via static code inspection (function (); {; dplyr::filter(mtcars, cyl == 4); }). Failed to locate global object in the relevant environments: 'cyl'

This can be fixed with two things: defining the variable as NULL

example <- function() {
  cyl <- NULL
  mtcars |>
    dplyr::filter(cyl == 4)
}

globals::globalsOf(example, mustExist = T)
# No error thrown...

or using pronouns.

example <- function() {
  mtcars |>
    dplyr::filter(rlang::.data$cyl == 4)
}

globals::globalsOf(example, mustExist = T)
# No error thrown...

However, when a global variable is defined using the utils::globalVariables function, the globals::globalsOf function throws an error.

example <- function() {
  utils::globalVariables(c("cyl"))

  mtcars |>
    dplyr::filter(cyl == 4)
}

globals::globalsOf(example, mustExist = T)
#> Error in globalsByName(names, envir = envir, mustExist = mustExist): Identified global objects via static code inspection (function (); {; utils::globalVariables(c("cyl")); dplyr::filter(mtcars, cyl == 4); }). Failed to locate global object in the relevant environments: 'cyl'

This is especially painful when developing R Shiny applications as a package.

I think adding this functionality would be a great addition, because is recommended in the book R Packages (2e) and in my humble opinion, it makes sense as the variable is defined globally.

What do you guys think? :)

@schloerke
Copy link

schloerke commented Oct 11, 2023

because is recommended in the book R Packages (2e)

From R Packages (2e)...

The “no visible binding” note is a peculiarity of using dplyr and unquoted variable names inside a package, where the use of bare variable names (english and temp) looks suspicious. You can add either of these lines to any file below R/ to eliminate this note

Using utils::globalVariables() is there to "eliminate this note". It does not actually set a global value that can be found.


Previous issue on finding globals given non-standard evaluation: #35


I don't believe utils::globalVariables() should be supported by {globals} even though the name implies "setting a globally available name".

@MichalLauer
Copy link
Author

Hello @schloerke, understood. Thanks for the quick answer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants