The $compile() method of a CmdStanModel object translates the Stan program to C++ and creates a compiled executable. In most cases the user does not need to explicitly call the$compile() method as compilation will occur when calling cmdstan_model(). However it is possible to set compile=FALSE in the call to cmdstan_model() and subsequently call the $compile() method directly. After compilation, the paths to the executable and the .hpp file containing the generated C++ code are available via the$exe_file() and $hpp_file() methods. The default is to create the executable in the same directory as the Stan program and to write the generated C++ code in a temporary directory. To save the C++ code to a non-temporary location use$save_hpp_file(dir).

compile(
quiet = TRUE,
dir = NULL,
pedantic = FALSE,
include_paths = NULL,
cpp_options = list(),
stanc_options = list(),
force_recompile = FALSE,
)

## Arguments

quiet (logical) Should the verbose output from CmdStan during compilation be suppressed? The default is TRUE, but if you encounter an error we recommend trying again with quiet=FALSE to see more of the output. (string) The path to the directory in which to store the CmdStan executable (or .hpp file if using $save_hpp_file()). The default is the same location as the Stan program. (logical) Should pedantic mode be turned on? The default is FALSE. Pedantic mode attempts to warn you about potential issues in your Stan program beyond syntax errors. For details see the Pedantic mode chapter in the Stan Reference Manual. Note: to do a pedantic check for a model that is already compiled use the$check_syntax() method instead. (character vector) Paths to directories where Stan should look for files specified in #include directives in the Stan program. (list) Any makefile options to be used when compiling the model (STAN_THREADS, STAN_MPI, STAN_OPENCL, etc.). Anything you would otherwise write in the make/local file. (list) Any Stan-to-C++ transpiler options to be used when compiling the model. See the Examples section below as well as the stanc chapter of the CmdStan Guide for more details on available options: https://mc-stan.org/docs/cmdstan-guide/stanc.html. (logical) Should the model be recompiled even if was not modified since last compiled. The default is FALSE. Deprecated and will be removed in a future release. Please turn on threading via cpp_options = list(stan_threads = TRUE) instead.

## Value

The $compile() method is called for its side effect of creating the executable and adding its path to the CmdStanModel object, but it also returns the CmdStanModel object invisibly. After compilation, the$exe_file(), $hpp_file(), and$save_hpp_file() methods can be used and return file paths.

The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.

The Stan and CmdStan documentation:

## Examples

# \dontrun{ file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") # by default compilation happens when cmdstan_model() is called. # to delay compilation until calling the $compile() method set compile=FALSE mod <- cmdstan_model(file, compile = FALSE) mod$compile()
#> Model executable is up to date!
mod$exe_file() #> [1] "/Users/jgabry/.cmdstanr/cmdstan-2.25.0/examples/bernoulli/bernoulli" # turn on threading support (for using functions that support within-chain parallelization) mod$compile(force_recompile = TRUE, cpp_options = list(stan_threads = TRUE))
#> Compiling Stan program...
mod\$exe_file()