Skip to content
Prev 11080 / 12125 Next

[R-pkg-devel] conditional import of a package?

Adelchi,

The package 'PortfolioAnalytics', which we maintain, uses this pattern
extensively to support different optimization solvers. ?The packages
are all in 'Suggests' rather than 'Imports', because we don't want them
all required all the time.

So the code pattern is something like this:

if(optimize_method=="pso"){
? ?stopifnot("package:pso" %in% search() ||
requireNamespace("pso",quietly = TRUE) )
? # ...
}

where all the code that requires the 'pso' engine in this example is
contained inside the if block and the stopifnot check.

Regards,

Brian