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

Automatically check that user code references exported, local, or attached package variables #26

Open
mtmorgan opened this issue Nov 6, 2013 · 2 comments

Comments

@mtmorgan
Copy link
Collaborator

mtmorgan commented Nov 6, 2013

Systematically integrate function to check on non-local use into bplapply and friends

@DarwinAwardWinner
Copy link

The foreach package has a pretty good heuristic for auto-exporting variables from the user's session:

> library(foreach)
> library(doParallel)
> library(parallel)
> cl <- makeForkCluster()
> registerDoParallel(cl)
> 
> f <- foreach(x=1:10, .combine=c, .verbose=TRUE)
> myvar <- 5
> myfunc <- function(i) i+ myvar
> myfunc2 <- function(j) myfunc(j)
> f %dopar% myfunc2(x)
automatically exporting the following variables from the local environment:
  myfunc, myfunc2, myvar 
...
 [1]  6  7  8  9 10 11 12 13 14 15

Notice that it fully follows the trail of function calls all the way to myvar. It is implemented in foreach::getexports, foreach:::getsyms, foreach:::expandsyms. You can see how it is used in e.g. doParallel:::doParallelSNOW.

@HenrikBengtsson
Copy link
Contributor

Just dropping by here to mention the globals package, which was created to attack these type of problems. The objective is to have one go-to package that provides a unified approach for static code inspection tailored to identifying globals/unknowns that need to be frozen/exported in asynchronous processing.

The package is currently leveraging the code walkers of the codetools package. Since codetools is rather conservative when it comes to identifying globals, and for our purposes we can allow ourselves to be much more liberal ("not much damage is done if we export a few falsely identified global variables"), I'm predicting that at some point globals will implement its own code walkers.

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

No branches or pull requests

3 participants