[Rd] Best way to implement optional functions?

Brian G. Peterson brian at braverock.com
Thu Oct 22 22:08:52 CEST 2015

On Thu, 2015-10-22 at 15:55 -0400, Duncan Murdoch wrote:
> I'm planning on adding some new WebGL functionality to the rgl package, 
> but it will pull in a very large number of dependencies. Since many 
> people won't need it, I'd like to make the new parts optional.
> The general idea I'm thinking of is to put the new stuff into a separate 
> package, and have rgl "Suggest" it.  But I'm not sure whether these 
> functions  should only be available in the new package (so users would 
> have to attach it to use them), or whether they should be in rgl, but 
> fail if the new package is not available for loading.
> Can people suggest other packages that solve this kind of problem in a 
> good way?

We have a number of functions in PerformanceAnalytics or
PortfolioAnalytics that rely on packages in 'Suggests'.

The model we have chiefly used is 

stopifnot("package:MASS" %in% search() || require("MASS",quietly=TRUE))

of course, it is now no longer recommended to do it this way, so we'll
probably need to test and load namespaces instead.

It makes sense to me to have such functions in the main package, and
provide pointers in the documentation and the error message which
describe that the other package is required to be available for the
function to work.



Brian G. Peterson
Ph: 773-459-4973
IM: bgpbraverock

More information about the R-devel mailing list