I built a R package whose some functions use the V8 package. But V8 is not supported on some platforms, so I want to make these functions available only for the platforms supporting V8. How to deal with this situation? I can put V8 in the Suggests field of DESCRIPTION instead of the Imports field, and test whether it is available with requireNamespace, but then how do I deal with the functions that must be imported from V8? I want to submit this package to CRAN.
CRAN package with an optional dependency
257 Views Asked by Stéphane Laurent At
2
There are 2 best solutions below
0
On
The cleanest solution in my opinion is to have two packages. The first package (A) would contain all of your code that does not depend (directly or indirectly) on V8. The second package (B) would depend on the A and contain all of your code that does require V8.
+-------+
| |
| V8 |
| |
+---^---+
|
| Requires
|
+-------+ +---+---+
| | | |
| A <------------+ B |
| | Requires | |
+-------+ +-------+
On platforms that support V8 then users can take B and on all other platforms users can take A.
Package A could Suggest package B.
I found a solution by copying the way used by the
reactRpackage.Put
V8in theSuggestsfield.Do not import
V8or its functions inNAMESPACE; useV8::...to use theV8functions.In the functions requiring
V8, userequireNamespaceto check whetherV8is present, and throw a message or an error if it is not:I ran
R CMD CHECKand it did not complain.