Proper way to handle optional package dependencies

巧了我就是萌 提交于 2019-12-23 10:15:42

问题


In a package I'm developing, several different methods of estimating are provided. Typically, each of these depends on functionality provided by other packages (in some case, with version requirements).

Up to now, I've simply put all those packages in the "depends" section of my description file, but the number of packages that my own package now "depends" upon, even though for most users only one of those will ever be relevant, so I was hoping that packages could be installed/loaded only as needed? The documentation on writing R packages is sometimes a bit terse and has changed somewhat since recent R versions, so perhaps anyone here can provide the latest on that?

Just to illustrate, this is the typical pattern:

doSomethingImportant<-function(params, workerFunction)
{
   #blabla
   workerFunction(partofparams)
   #moreblabla
}

and then I'd have

wfA<-function(partofparams)
{
   #use something from package A
}

and

wfB<-function(partofparams)
{
   #use something from package B
}

And the user would call this function something like:

result<-doSomethingImportant(params, wfA)

With each user typically having a preference for one of the wfX. Ideally, when the user (first) uses one of the wfX, I'd like to have it installed/loaded on demand, but if that's not possible, I'd like a warning as soon as possible that it's going to fail (in fact, in my case, a lot of preparation may come before any actual attempt to call workerFunction from doSomethingImportant, which will all be lost if in the end the right package was not already present.

Can you suggest how to handle this properly and as user-friendly as possible?

来源:https://stackoverflow.com/questions/15545869/proper-way-to-handle-optional-package-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!