A portable Stan-backend layer for R packages that fit a Stan model. It gives a host package one interface for fitting through either rstan or (optionally) cmdstanr, so the same code works whichever backend is installed.
flexstanr compiles no Stan of its own: the host package supplies its own compiled models, and flexstanr resolves them from the calling package at run time.
stan_options() collects and validates sampler arguments
for the chosen backend, forwarding them verbatim so
calls feel native. Mixing one backend’s argument vocabulary into the
other errors with a “did you mean” hint.fit_model() dispatches the fit to the backend recorded
on the options, resolving the compiled model by name from the calling
package.backend_draws_array(), backend_extract(),
backend_generate_quantities(), and
backend_has_draws().flexstanr is not yet on CRAN. Install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("ACCIDDA/flexstanr")The default backend, rstan, is a hard dependency. cmdstanr is an optional backend; it is not on CRAN, so a project that wants it installs it separately (see the cmdstanr getting-started guide).
Declare flexstanr as a dependency and call it from your own fitting code:
# in your package
opts <- flexstanr::stan_options(chains = 4, iter = 2000, seed = 1)
fit <- flexstanr::fit_model(
"coverage", # resolved from your package's stanmodels / inst/stan
dat_stan = data_list,
init = init_list,
stan_opts = opts
)
draws <- flexstanr::backend_draws_array(fit)The model name is resolved against your package’s own compiled models
(stanmodels for rstan,
inst/stan/<name>.stan for cmdstanr); the calling
package is detected automatically. Run
flexstanr::use_flexstanr() once from your package root to
wire the dependencies into its DESCRIPTION.
Note. flexstanr began as a
use_standalone()script vendored into the ACCIDDA Stan packages (imuGAP, hestia, SeverityEstimate). It is now a proper package those consumers import rather than copy. See the flexstanr 0.1.0 CRAN release milestone.
MIT (c) ACCIDDA.