Importing snowfall into custom R package
Asked Answered
D

2

8

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.

Dinka answered 29/3, 2016 at 12:43 Comment(5)
System setup, versions, actual code to do those operations? Looks like pkg:snow is not being attached properly.Cacophony
I have snow in the description file and I can see an import(snow) in the namespace. I also added an #' @import snow to the function as well. Is there something else I should be doing?Dinka
If I explicitly call snow in the wrapper code itself, the package works fine.Dinka
Was there any solution to this (other than add snow to imports section)? I'm having the same problem. Using Roxygen2 with importFrom snow setDefaultClusterOptions but returning same error.Increment
... as an aside are you getting more out of snowfall than it is costing you? As a usability wrapper I never felt like it improved my experience... but maybe I was already too far down the rabbit hole by the time I stumbled onto it. Unless you have some specialized reasons for wanting to use snowfall you may want to consider cran.r-project.org/web/packages/future/index.html. It is under active development and the author seems dedicated to supporting a pretty broad variety of backends.Mejia
M
5

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.

Mejia answered 12/12, 2017 at 3:36 Comment(0)
O
3

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

Oe answered 29/3, 2016 at 13:15 Comment(3)
I have snow in the description file and I can see an import(snow) in the namespace. I also added an #' @import snow to the function as well. Is there something else I should be doing?Dinka
If I explicitly call snow in the wrapper code itself, the package works fine.Dinka
I would import only the functions you need for a package rather than importing the entire package. Use '# @importFrom snow setDefaultClusterOptions if you use roxygen2Oe

© 2022 - 2024 — McMap. All rights reserved.