foreach icon indicating copy to clipboard operation
foreach copied to clipboard

foreach in packages and tests

Open latot opened this issue 3 years ago • 2 comments

Hi, actually I can't found doc, to how use this package in tests, seems it does not works very well with rcmdcheck.

foreach::foreach(x = iterators::iter(matrix, by = "col"), .combine = 'cbind', .packages = 'dplyr') %dopar% {
 x
}

Then the message:

❯ checking R code for possible problems ... NOTE
Undefined global functions or variables:
    x

We can use globalVariables(), but is a very bad idea, can even hide problems, I notice there is tests in this package and rcmdcheck works!, I would like to know how to use this package in packages, and ideally add the data to the docs plis.

Thx!

latot avatar Jul 22 '22 14:07 latot

FWIW, you can do:

y <- foreach::foreach(x = 1:3) %dopar% {
  ## To please 'R CMD check'
  x <- x

  sqrt(x)
}

That tricks codetools to not "see" x as a global variable. Note, however, it could be that one day this trick won't work anymore.

Alternatively, you can do:

## To please 'R CMD check'
x <- NULL

y <- foreach::foreach(x = 1:3) %dopar% {
  sqrt(x)
}

HenrikBengtsson avatar Jul 23 '22 20:07 HenrikBengtsson

Great!, working even now :3

Maybe add this to the doc (?

latot avatar Aug 02 '22 17:08 latot