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 -- Brian G. Peterson ph: +1.773.459.4973 im: bgpbraverock On Wed, 2024-12-04 at 19:25 +0100, Adelchi Azzalini wrote: > Hi. I am working on the development of an existing package (smof, on > CRAN). My current aim is widen the list of possible optimizers from > which the user can select one method for optimizing a certain task. > Well-known possibilities within the base package are optim (with > various options) and nlminb. Besides these, I am thinking of > including also those of package nloptr, but without forcing users to > install this package which perhaps they don't need for other > purposes. Hence, I would like to import nloptr only if it is > available on the user system; it not, I can just confine the list of > optimizers to optim and nlminb. > > This idea implies a sort of “conditional import” of nloptr. Is this > possible? Section 1.1.3.1 "Suggested packages" of > https://translation.r-project.org/man/R-exts/R-exts-ko.html#Suggested-packages > seems to hint at such a possibility. See the use of requireNamespace > in the second paragraph. > > After elaborating along this line, I packaged my code, with nloptr > listed on the line Suggests of DESCRIPTION. However this attempt > failed a the “R CMD check “ stage with message > > Namespace dependency missing from DESCRIPTION Imports/Depends > entries: ‘nloptr’ > > In addition, I have no idea of how to declare a "conditional import” > in NAMESPACE. > > Is this idea of “conditional import” totally unfeasible, then? > > --- > Adelchi Azzalini > http://azzalini.stat.unipd.it > > ______________________________________________ > R-package-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-package-devel > [[alternative HTML version deleted]] ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel