The $expose_functions() method of a CmdStanModel object
will compile the functions in the Stan program's functions block and
expose them for use in R. This can also be specified via the
compile_standalone argument to the $compile()
method.
This method is also available for fitted model objects (CmdStanMCMC, CmdStanVB, etc.).
See Examples.
Note: there may be many compiler warnings emitted during compilation but these can be ignored so long as they are warnings and not errors.
expose_functions(global = FALSE, verbose = FALSE)The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.
The Stan and CmdStan documentation:
Stan documentation: mc-stan.org/users/documentation
CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide
Other CmdStanModel methods:
model-method-check_syntax,
model-method-compile,
model-method-diagnose,
model-method-format,
model-method-generate-quantities,
model-method-laplace,
model-method-optimize,
model-method-pathfinder,
model-method-sample,
model-method-sample_mpi,
model-method-variables,
model-method-variational
if (FALSE) { # \dontrun{
stan_file <- write_stan_file(
"
functions {
real a_plus_b(real a, real b) {
return a + b;
}
}
parameters {
real x;
}
model {
x ~ std_normal();
}
"
)
mod <- cmdstan_model(stan_file)
mod$expose_functions()
mod$functions$a_plus_b(1, 2)
fit <- mod$sample(refresh = 0)
fit$expose_functions() # already compiled because of above but this would compile them otherwise
fit$functions$a_plus_b(1, 2)
} # }