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

False positive "failed to locate global object" when using NSE #65

Open
DavisVaughan opened this issue Sep 22, 2020 · 2 comments
Open

Comments

@DavisVaughan
Copy link
Contributor

In the below example, I would not have expected any failures

library(globals)

fn <- function(expr) {
  expr <- substitute(expr)
  eval(expr, envir = mtcars)
}

fn(cyl)
#>  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

expr <- quote(fn(cyl))

globalsOf(expr)
#> Error in globalsByName(names, envir = envir, mustExist = mustExist): Identified global objects via static code inspection (fn(cyl)). Failed to locate global object in the relevant environments: 'cyl'

This is tough because you probably search for cyl in the function environment or the global environment, but it is actually evaluated in the context of mtcars through the eval()

This came up on SO as a furrr issue related to dplyr and rlang
https://stackoverflow.com/questions/64006043/using-tidy-evaluations-with-furrr

@DavisVaughan
Copy link
Contributor Author

Actually it looks like future::getGlobalsAndPackages() is passing mustExist = FALSE down to globals, so in theory it should have "worked" already.

I think this is the true issue:

library(globals)

fn <- function(expr) {
  globalsByName("expr", envir = environment(), mustExist = FALSE)
}

fn(cyl)
#> Error in get(name, envir = env, inherits = FALSE): object 'cyl' not found

# Really `globalsByName()` does this, but `expr` shouldn't be evaluated here! It's lazy!
fn2 <- function(expr) {
  get("expr", envir = environment())
}

fn2(cyl)
#> Error in get("expr", envir = environment()): object 'cyl' not found

@DavisVaughan
Copy link
Contributor Author

DavisVaughan commented Sep 22, 2020

I am also now remembering that in furrr this is a "common gotcha". Each argument to the function you are going to parallelize has to be evaluated once so it can be shipped off to the worker.

So maybe this is something the user has to live with?

See my notes here https://davisvaughan.github.io/furrr/articles/articles/gotchas.html#non-standard-evaluation-of-arguments

I've added a second answer to the SO post
https://stackoverflow.com/a/64009850/7394743

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

1 participant