-
Notifications
You must be signed in to change notification settings - Fork 26
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
Global variables in functions cannot be found #43
Comments
A PR would be appreciated. I don't fully understand what's going on yet. The |
Well, my
I found How R Searches and Finds Stuff useful while looking into this. |
That makes sense. It would be nice to follow a purley I think I tried the |
Josh, you still working on this?
I may take a crack at improving foreach static code analysis generally
soon...
On Nov 30, 2016 11:45, "Joshua Ulrich" <[email protected]> wrote:
Well, my doParallel example only works if you call clusterExport first,
which assigns the objects to the global environment of the nodes by
default. Nodes created by doMC essentially have a copy of the master
process, so the objects are in the global environment on the node too.
doRedis::setExport assigns the objects to the .doRedisGlobals$exportenv,
not the global environment like the other two. Since
.doRedisGlobals$exportenv isn't on the search path, objects in it cannot be
found by lexical scope if they're used inside a user-defined function
(because the user cannot define the function in .doRedisGlobals$exportenv;
it doesn't exist until they call foreach).
I found How R Searches and Finds Stuff
<http://blog.obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/> useful
while looking into this.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#43 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAIsnvL_IkRp4_o74To1qUxQrO6iY3Ijks5rDagNgaJpZM4K9-zj>
.
|
Granted, using global variables in functions isn't the best practice, but sometimes users do it. In this case,
doRedis
does not behave as do otherforeach
backends, which caught me off-guard. For example:I've tracked this down to the evaluation in
doRedis:::.evalWrapper()
. The expression.doRedisGlobals$expr
is evaluated using the.doRedisGlobals$exportenv
environment, which contains all the exported variables. But.doRedisGlobals$exportenv
is not on the search path, sox
it cannot be found whenf()
is called. That's becausef()
's environment is the global environment, and whenx
cannot be found there, R starts to walk the search path. Note that this is only a problem when the function is invoked viaeval
.It seems like you tried to address something like this already. But I don't think that works in this case, because the evaluation environments are not searched when
f()
is called. Onlyf()
's enclosing environments are searched. Since the documentation warns thatparent.env<-
may be removed, perhaps the next best solution isattach(.doRedisGlobals$exportenv)
?If you like, I'm happy to make that change, test using my actual use case, and submit a PR.
The text was updated successfully, but these errors were encountered: