Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing snowfall into custom R package

Tags:

r

snowfall

I'm developing an R package which needs to use parallelisation as made available by the snowfall package. snowfall doesn't seem to import the same was as other packages like ggplot2, data.table, etc. I've included snowfall, rlecuyer, and snow in the description file, name space file, and as an import argument in the function itself. When I try to access this function, I get the following error:

Error in sfInit() : could not find function "setDefaultClusterOptions"

The sfInit function seems to have a nostart / nostop argument which it says is related to nested usage of sfInit but that doesn't seem to do the trick for me either.

The actual code itself uses an sfInit (which is where I get the error), some sfExports and sfLibrarys, and an sfLapply.

Possible solution: It seems to work if I move snow from the import section to the depends section in the Desciption file. I don't know why though.

like image 567
TheComeOnMan Avatar asked Mar 29 '16 12:03

TheComeOnMan


2 Answers

When you include a package in 'Depends' when one attaches your package they also attach the package on which your package Depends to their namespace.

This and other differences between Depends and Imports is explained well in other questions on this site.

If you look at {snowfall}'s DESCRIPTION you'll see that it Depends on {snow}. It is plausible that the authors of snowfall know something we don't and that {snow} has to be attached to the global search path in order to work. In fact that is the top caveat in the top answer to the question I linked above...

... if your package relies on a package A which itself "Depends" on another package B, your package will likely need to attach A with a "Depends directive.

This is because the functions in package A were written with the expectation that package B and its functions would be attached to the search() path.

So, in your case, it just so happens that all {snowfall} wants is {snow} and you happened to provide it. However, it appears the more correct behavior may be for you to Depend on {snowfall} directly.

like image 95
russellpierce Avatar answered Nov 07 '22 00:11

russellpierce


setDefaultClusterOptions is a function from the snow package. You need to import that too.

like image 37
Thierry Avatar answered Nov 07 '22 02:11

Thierry