From murdoch at stats.uwo.ca  Sat Jun 13 22:50:59 2009
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Sat, 13 Jun 2009 16:50:59 -0400
Subject: [Rd] conditional dependencies & loading
In-Reply-To: <c2350ba40906131339g69cd80d9k8619c5dc83734242@mail.gmail.com>
References: <c2350ba40906131339g69cd80d9k8619c5dc83734242@mail.gmail.com>
Message-ID: <4A341133.4090402@stats.uwo.ca>

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
>



[Rd] conditional dependencies & loading
Duncan Murdoch