2

I'm in the process of writing an R package. One of my functions takes another function and some other data-related arguments and runs a %dopar% loop using the foreach package. This foreach-using function is used inside the one of the main functions of my package.

When I call the main function from another file, after having loaded my package, I get the message

 Error in { : task 1 failed - "could not find function "some_function"

where some_function is some function from my package. I get this message, with varying missing function, when I set the .export argument in the call to foreach to any of the following:

  • ls(as.environment("package:mypackagename"))
  • ls(.GlobalEnv)
  • ls(environment())
  • ls(parent.env(environment()))
  • ls(parent.env(parent.env(environment()))

And even concatenations of the above. I also tried passing my package name to the .package argument, which only yields the error message

Error in e$fun(obj, substitute(ex), parent.frame(), e$data) : worker initialization failed: there is no package called ‘mypackagename’

I feel like I have tried just about everything, and I really need this piece of code to work. I should note that it does work if I use %do% instead of %dopar%. What am I doing wrong?

Benjamin Allévius
  • 817
  • 1
  • 7
  • 14
  • 4
    There's a lot of ways this could be going wrong, and it's unlikely we can help you unless you show us some of your code. At the bare minimum, we need to see the smallest part of your function that uses `%dopar%` that still gives the error (simplify!), and we need to see how you call that function. Better still, a full not-quite-working example we can play with. Check [here.](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Bryan Hanson Apr 11 '15 at 16:02
  • Hmm, will try to put something together. I was thinking that the fact that it's a package-related issue (functions exist in the environment created by the package namespace, and are not visible in the global environment) makes it difficult to reproduce the situation. – Benjamin Allévius Apr 11 '15 at 16:16
  • Many times I find that when I distill things down to a minimal working example that I find the problem myself - I must have started 20 questions over several years which I ultimately never had to ask. – Bryan Hanson Apr 11 '15 at 16:21

0 Answers0