Search code examples
rv8cran

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.


Solution

  • 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.