[Rd] conditional dependencies & loading

Duncan Murdoch murdoch at stats.uwo.ca
Sat Jun 13 22:50:59 CEST 2009


Kynn Jones wrote:
> Hi!
>
> I'm working on a package that must convert data to and from JSON.  For this,
> it can use either the rjson package, or preferably, the faster RJSONIO
> package.
>
> I have two related questions about this.
>
> First, how can I specify that the package depends on *either* RJSONIO *or*
> rjson?  (I.e. both are not required.)
>   

I don't think you can do that.  You can say it suggests both, but it 
will not automatically fail to install if they are not found.  You need 
to put a check into your .onLoad function to fail with an error if 
neither is found.
> Second, what's the best-practice R idiom for such conditional loading?  I.e.
> I'm looking for R's equivalent of Python's
>
>   
>>>> try:
>>>>         
> ...     import json
> ... except ImportError:
> ...     import simplejson as json
>
>   

You would use something like

if (!require(RJSONIO) && !require(rjson)) stop("Need one of RJSONIO or 
rjson")

in your .onLoad function.

Duncan Murdoch
> Thanks!
>
> kynn
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list