[R] R CMD check and foreach code
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Mar 27 17:55:54 CET 2014
On 27/03/2014 12:46 PM, Jannis wrote:
> On 27.03.2014 15:58, Duncan Murdoch wrote:
> > 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
> >
>
> Thanks, Duncan, for your reply. So how would I use this now? Reading the
> documentation still leaves me puzzled .... Just adding
> globalVariables('i') to the dummy function like this:
>
> dummy = function() {
>
> globalVariables('i')
>
> dummyfun = function(iter) {
> iter * 2
> }
> registerDoMC(4)
> results = foreach(i = 1:10) %dopar% dummyfun(iter = i)
> }
>
>
> does not solve the problem.
The call needs to be at the global level, i.e. outside the function.
Duncan Murdoch
More information about the R-help
mailing list