Title: | Pre-Compiled 'CmdStan' Models in R Packages |
---|---|
Description: | Similar to 'rstantools' for 'rstan', the 'instantiate' package builds pre-compiled 'CmdStan' models into CRAN-ready statistical modeling R packages. The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of 'cmdstanr' without any additional compilation after package installation. This approach saves time and helps R package developers migrate from 'rstan' to the more modern 'cmdstanr'. Packages 'rstantools', 'cmdstanr', 'stannis', and 'stanapi' are similar Stan clients with different objectives. |
Authors: | William Michael Landau [aut, cre] , Eli Lilly and Company [cph, fnd] |
Maintainer: | William Michael Landau <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.3 |
Built: | 2024-11-02 06:42:48 UTC |
Source: | CRAN |
Similar to rstantools
for rstan
,
the instantiate
package builds pre-compiled
CmdStan
models into CRAN-ready statistical modeling R packages.
The models compile once during installation,
the executables live inside the file systems of their
respective packages, and users have the full power
and convenience of CmdStanR
without any additional compilation after package installation.
This approach saves time and helps R package developers
migrate from rstan
to the more modern CmdStanR
.
instantiate
can find CmdStanR and CmdStan.Check if instantiate
can find CmdStanR and CmdStan.
stan_cmdstan_exists( cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = "") )
stan_cmdstan_exists( cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = "") )
cmdstan_install |
Character of length 1, how to look for an installed copy of CmdStan. See https://wlandau.github.io/instantiate/ for details. Choices:
|
TRUE
if instantiate
can find the CmdStanR R package and the
CmdStan command line tool. Returns FALSE
otherwise.
Other cmdstan:
stan_cmdstan_version()
stan_cmdstan_exists()
stan_cmdstan_exists()
instantiate
Return the directory path to CmdStan that the instantiate
package uses.
stan_cmdstan_path(cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""))
stan_cmdstan_path(cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""))
cmdstan_install |
Character of length 1, how to look for an installed copy of CmdStan. See https://wlandau.github.io/instantiate/ for details. Choices:
|
Character of length 1, directory path to CmdStan. The path
may or may not exist. Returns the empty string ""
if the path
cannot be determined.
Other packages:
stan_package_clean()
,
stan_package_compile()
,
stan_package_configure()
,
stan_package_model_files()
stan_cmdstan_path()
stan_cmdstan_path()
instantiate
uses.Return the version of CmdStan that the instantiate
package uses.
stan_cmdstan_version( cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), error_on_NA = TRUE )
stan_cmdstan_version( cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), error_on_NA = TRUE )
cmdstan_install |
Character of length 1, how to look for an installed copy of CmdStan. See https://wlandau.github.io/instantiate/ for details. Choices:
|
error_on_NA |
Logical of length 1, whether an error should be thrown if CmdStan is not found. |
Character of length 1, version of CmdStan that the instantiate
package uses. If CmdStan is not found, then the return value depends
on the error_on_NA
argument.
Other cmdstan:
stan_cmdstan_exists()
if (stan_cmdstan_exists()) { message(stan_cmdstan_version()) }
if (stan_cmdstan_exists()) { message(stan_cmdstan_version()) }
Remove one or more compiled Stan models from the file system of an R package.
stan_package_clean(models = instantiate::stan_package_model_files())
stan_package_clean(models = instantiate::stan_package_model_files())
models |
Character vector of file paths to Stan model source code
files. Defaults to the Stan files in |
NULL
(invisibly). Called for its side effects.
Other packages:
stan_cmdstan_path()
,
stan_package_compile()
,
stan_package_configure()
,
stan_package_model_files()
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) stan_package_configure(path = path) models <- stan_package_model_files(path) list.files(file.path(path, "inst", "stan")) stan_package_compile(models) list.files(file.path(path, "inst", "stan")) # Clean up the compiled Stan model files: stan_package_clean(models = models) list.files(file.path(path, "inst", "stan")) }
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) stan_package_configure(path = path) models <- stan_package_model_files(path) list.files(file.path(path, "inst", "stan")) stan_package_compile(models) list.files(file.path(path, "inst", "stan")) # Clean up the compiled Stan model files: stan_package_clean(models = models) list.files(file.path(path, "inst", "stan")) }
Compile all Stan models in a directory, usually in a package.
stan_package_compile( models = instantiate::stan_package_model_files(), cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), verbose = TRUE, quiet = FALSE, pedantic = FALSE, include_paths = NULL, user_header = NULL, cpp_options = list(), stanc_options = list(), force_recompile = getOption("cmdstanr_force_recompile", default = FALSE), threads = FALSE, ... )
stan_package_compile( models = instantiate::stan_package_model_files(), cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), verbose = TRUE, quiet = FALSE, pedantic = FALSE, include_paths = NULL, user_header = NULL, cpp_options = list(), stanc_options = list(), force_recompile = getOption("cmdstanr_force_recompile", default = FALSE), threads = FALSE, ... )
models |
Character vector of file paths to Stan model source code
files. Defaults to the Stan files in |
cmdstan_install |
Character of length 1, how to look for an installed copy of CmdStan. See https://wlandau.github.io/instantiate/ for details. Choices:
|
verbose |
Logical of length 1, whether to set the
|
quiet |
Argument to |
pedantic |
Logical of length 1, whether to activate pedantic
mode when compiling the Stan models. See the |
include_paths |
Argument to |
user_header |
Argument to |
cpp_options |
Argument to |
stanc_options |
Argument to |
force_recompile |
Argument to |
threads |
Argument to |
... |
Other named arguments to |
If building a package using instantiate
, all Stan model files
must live in a folder called src/stan/
in the package source
directory.
NULL
(invisibly). Called for its side effects.
Other packages:
stan_cmdstan_path()
,
stan_package_clean()
,
stan_package_configure()
,
stan_package_model_files()
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) stan_package_configure(path = path) models <- stan_package_model_files(path) list.files(file.path(path, "inst", "stan")) stan_package_compile(models) list.files(file.path(path, "inst", "stan")) }
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) stan_package_configure(path = path) models <- stan_package_model_files(path) list.files(file.path(path, "inst", "stan")) stan_package_compile(models) list.files(file.path(path, "inst", "stan")) }
Write configuration files in an R package which
compile all the Stan models in src/stan/
when the package installs.
stan_package_configure(path = getwd(), overwrite = FALSE)
stan_package_configure(path = getwd(), overwrite = FALSE)
path |
Character of length 1, file path to the package which will
contain Stan models in |
overwrite |
Logical of length 1, whether to overwrite any existing configuration files. |
Writes configuration scripts configure
and configure.win
in the directory specified by the path
argument.
NULL
(invisibly). Called for its side effects.
Other packages:
stan_cmdstan_path()
,
stan_package_clean()
,
stan_package_compile()
,
stan_package_model_files()
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) list.files(path) stan_package_configure(path = path) list.files(path) }
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) list.files(path) stan_package_configure(path = path) list.files(path) }
Create an example package with a Stan model inside.
stan_package_create(path = tempfile())
stan_package_create(path = tempfile())
path |
Path to write the package. Must not already exist. |
After creating the package, the next step is to
configure it with stan_package_configure()
. After that,
install it as an ordinary R package to automatically compile the models.
NULL
(invisibly). Called for its side effects.
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) list.files(path) }
if (identical(Sys.getenv("INSTANTIATE_EXAMPLES"), "true")) { path <- tempfile() stan_package_create(path = path) list.files(path) }
Get the cmdstanr
model object for a pre-compiled Stan
model inside a package.
stan_package_model( name, package, library = NULL, cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), compile = FALSE, ... )
stan_package_model( name, package, library = NULL, cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = ""), compile = FALSE, ... )
name |
Character of length 1, base name of the Stan model file
(without the containing directory or |
package |
Character of length 1, name of the R package to look for the built-in Stan model. |
library |
Character of length 1 or |
cmdstan_install |
Character of length 1, how to look for an installed copy of CmdStan. See https://wlandau.github.io/instantiate/ for details. Choices:
|
compile |
|
... |
Named arguments passed via |
Packages configured with instantiate
compile their Stan
models on installation. Then the stan_package_model()
function
retrieves the cmdstanr::cmdstan_model()
object without needing
to re-compile the model. Please see the documentation website
of the instantiate
package for examples.
An R6
Stan model object from the cmdstanr
package.
Please visit the documentation website at https://mc-stan.org/cmdstanr/
for detailed information on the composition of this model object
and how to use it to conduct Bayesian inference.
# Please see the documentation website of the {instantiate} package # for examples.
# Please see the documentation website of the {instantiate} package # for examples.
List all the paths to the Stan model files in a package.
stan_package_model_files(path = getwd())
stan_package_model_files(path = getwd())
path |
Character of length 1, root path to start from when searching for Stan model files. |
All Stan models must live in the src/stan/
directory in the
package file system.
Character vector of paths to Stan model files in the package.
Other packages:
stan_cmdstan_path()
,
stan_package_clean()
,
stan_package_compile()
,
stan_package_configure()
path <- tempfile() stan_package_create(path = path) stan_package_model_files(path)
path <- tempfile() stan_package_create(path = path) stan_package_model_files(path)