model-method-expose_functions: Expose Stan functions to R

model-method-expose_functionsR Documentation

Expose Stan functions to R

Description

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.

Usage

expose_functions(global = FALSE, verbose = FALSE)

Arguments

global

(logical) Should the functions be added to the Global Environment? The default is FALSE, in which case the functions are available via the functions field of the R6 object.

verbose

(logical) Should detailed information about generated code be printed to the console? Defaults to FALSE.

See Also

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

The Stan and CmdStan documentation:

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

Examples

## Not run: 
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)

## End(Not run)



stan-dev/cmdstanr documentation built on May 16, 2024, 12:58 a.m.