CRAN提交:含可选依赖V8的R包平台适配方案问询
Great question! Handling optional dependencies like V8 (which has platform limitations) while sticking to CRAN's rules is a common scenario, and your initial plan is already heading in the right direction. Let's break down the proper implementation step by step:
- Move
V8fromImportstoSuggests(don’t include it inImportsat all—since it’s optional, CRAN requires it to live inSuggests). - Optionally, add a clear note in the
Descriptionfield to set user expectations:Description: A package for [your core functionality]. Advanced features require the V8 package, which is not supported on all platforms.
- Do not add
import(V8)orimportFrom(V8, ...)to your NAMESPACE file. CRAN will reject your package if you attempt to unconditionally import an optional dependency. - Instead, reference V8 functions directly using the double-colon syntax (
V8::function_name) whenever you need them in your code.
For every function that relies on V8, add a check at the start to verify if V8 is installed and usable. If not, throw a clear, user-friendly error. To avoid repetition, create a helper function to reuse this check across multiple functions.
Example Helper Check Function:
.check_v8_available <- function() { if (!requireNamespace("V8", quietly = TRUE)) { stop( "This function requires the 'V8' package.\n", "Please install it with: install.packages('V8')\n", "Note: V8 is not supported on all platforms.", call. = FALSE ) } }
Example V8-Dependent Function:
#' Evaluate JavaScript Code (Requires V8) #' #' This function uses the V8 package to run JavaScript code. It only works if V8 #' is installed and supported on your platform. #' #' @param js_code Character string containing valid JavaScript code #' @return The result of evaluating the JavaScript code #' @export #' @examples #' \dontrun{ #' # This example needs V8 to be installed first #' eval_js("2 * Math.PI") #' } eval_js <- function(js_code) { .check_v8_available() ctx <- V8::v8() ctx$eval(js_code) }
- Test without V8: Run
R CMD check --no-suggestson your package to confirm it installs, loads, and passes all checks even when V8 is missing. - Document clearly: Wrap V8-dependent examples in
\dontrun{}—this tells CRAN’s check process to skip those examples when V8 isn’t available. - Avoid silent failures: Never let a function crash without explanation; always explicitly inform users about the dependency and platform limitation.
If you want to inform users upfront about V8 availability when loading your package, add a message in your .onLoad() function (this isn’t required by CRAN, but it’s a nice UX touch):
.onLoad <- function(libname, pkgname) { if (!requireNamespace("V8", quietly = TRUE)) { packageStartupMessage( "Note: V8 package not found. Some advanced functions will be unavailable.\n", "Install V8 with: install.packages('V8')" ) } }
This approach keeps your package fully CRAN-compliant, works seamlessly on platforms without V8, and only exposes V8-dependent functionality when the package is available and supported.
内容的提问来源于stack exchange,提问作者Stéphane Laurent

