Stéphane Laurent
Stéphane Laurent

Reputation: 84529

CRAN package with an optional dependency

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.

Upvotes: 2

Views: 258

Answers (2)

Stéphane Laurent
Stéphane Laurent

Reputation: 84529

I found a solution by copying the way used by the reactR package.

  • Put V8 in the Suggests field.

  • Do not import V8 or its functions in NAMESPACE; use V8::... to use the V8 functions.

  • In the functions requiring V8, use requireNamespace to check whether V8 is present, and throw a message or an error if it is not:

    if(!requireNamespace("V8")){
      message("This addin requires the 'V8' package.")
      return(invisible())
    }
    

I ran R CMD CHECK and it did not complain.

Upvotes: 3

sdgfsdh
sdgfsdh

Reputation: 37045

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.

Upvotes: 0

Related Questions