Re: [Rd] conditional dependencies & loading

From: Duncan Murdoch <>
Date: Sat, 13 Jun 2009 16:50:59 -0400

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]]
> ______________________________________________
> mailing list
> mailing list Received on Sat 13 Jun 2009 - 20:55:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 14 Jun 2009 - 07:35:56 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive