[R] R CMD check and foreach code

Duncan Murdoch murdoch.duncan at gmail.com
Thu Mar 27 15:58:20 CET 2014


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




More information about the R-help mailing list