Skip to content
Back to formatted view

Raw Message

Message-ID: <4A341133.4090402@stats.uwo.ca>
Date: 2009-06-13T20:50:59Z
From: Duncan Murdoch
Subject: conditional dependencies & loading
In-Reply-To: <c2350ba40906131339g69cd80d9k8619c5dc83734242@mail.gmail.com>

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
>