On 27/03/2014 10:29 AM, Jannis wrote:
Dear R users,


I have created a package and would like to check it for CRAN/R-Forge
submission. I use some parallelized code with the help of foreach and doMC.

R CMD check now gives me a warning even though my code is correctly
programmed (at least I think that it is). The following dummy function
inside a package produces the error:


dummy = function() {


dummyfun = function(iter) {
iter * 2
}
registerDoMC(4)
results = foreach(i = 1:10) %dopar% dummyfun(iter = i)
}


R CMD check --as-cran mypackage

now gives:

dummy: no visible binding for global variable ā€˜iā€™


It is clear that i can not be found as only the call to foreach creates
it (similar to a for loop), but should not the R check consider this? Or
is my programming poor? Setting i to any arbitrary value before running
foreach makes the error disappear, but this would feel as a really dirty
hack.

I would use a slightly less dirty hack: call globalVariables() to declare that i is global.

The foreach() function is using nonstandard evaluation to make this work, and codetools (that does the checks) doesn't know all the ins and outs of it. (foreach is in a contributed package, not base R.) The globalVariables() function is a way to tell codetools that even though it looks wrong, the variable is really being used safely.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to