The $format()
method of a CmdStanModel
object
runs stanc's auto-formatter on the model code. Either saves the formatted
model directly back to the file or prints it for inspection.
format(
overwrite_file = FALSE,
canonicalize = FALSE,
backup = TRUE,
max_line_length = NULL,
quiet = FALSE
)
(logical) Should the formatted code be written back
to the input model file. The default is FALSE
.
(list or logical) Defines whether or not the compiler
should 'canonicalize' the Stan model, removing things like deprecated syntax.
Default is FALSE
. If TRUE
, all canonicalizations are run. You can also
supply a list of strings which represent options. In that case the options
are passed to stanc (new in Stan 2.29). See the User's guide section
for available canonicalization options.
(logical) If TRUE
, create stanfile.bak backups before
writing to the file. Disable this option if you're sure you have other
copies of the file or are using a version control system like Git. Defaults
to TRUE
. The value is ignored if overwrite_file = FALSE
.
(integer) The maximum length of a line when formatting.
The default is NULL
, which defers to the default line length of stanc.
(logical) Should informational messages be suppressed? The
default is FALSE
.
The $format()
method returns TRUE
(invisibly) if the model
is valid.
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-expose_functions
,
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
# \dontrun{
# Example of fixing old syntax
# real x[2] --> array[2] real x;
file <- write_stan_file("
parameters {
real x[2];
}
model {
x ~ std_normal();
}
")
# set compile=FALSE then call format to fix old syntax
mod <- cmdstan_model(file, compile = FALSE)
mod$format(canonicalize = list("deprecations"))
#> Syntax error in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/model_1fc88c86300d78667dd3e476a636c279.stan', line 3, column 9 to column 10, parsing error:
#> -------------------------------------------------
#> 1:
#> 2: parameters {
#> 3: real x[2];
#> ^
#> 4: }
#> 5: model {
#> -------------------------------------------------
#>
#> ";" expected after variable declaration.
#> It looks like you are trying to use the old array syntax.
#> Please use the new syntax:
#> array[2] real x;
#> Error: Syntax error found! See the message above for more information.
# overwrite the original file instead of just printing it
mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE)
#> Syntax error in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/model_1fc88c86300d78667dd3e476a636c279.stan', line 3, column 9 to column 10, parsing error:
#> -------------------------------------------------
#> 1:
#> 2: parameters {
#> 3: real x[2];
#> ^
#> 4: }
#> 5: model {
#> -------------------------------------------------
#>
#> ";" expected after variable declaration.
#> It looks like you are trying to use the old array syntax.
#> Please use the new syntax:
#> array[2] real x;
#> Error: Syntax error found! See the message above for more information.
mod$compile()
#> Syntax error in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/model-59c028cea1fd.stan', line 3, column 9 to column 10, parsing error:
#> -------------------------------------------------
#> 1:
#> 2: parameters {
#> 3: real x[2];
#> ^
#> 4: }
#> 5: model {
#> -------------------------------------------------
#>
#> ";" expected after variable declaration.
#> It looks like you are trying to use the old array syntax.
#> Please use the new syntax:
#> array[2] real x;
#> make: *** [/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpiACQ3q/model-59c028cea1fd.hpp] Error 1
#> Error: An error occured during compilation! See the message above for more information.
# Example of removing unnecessary whitespace
file <- write_stan_file("
data {
int N;
array[N] int y;
}
parameters {
real lambda;
}
model {
target +=
poisson_lpmf(y | lambda);
}
")
mod <- cmdstan_model(file, compile = FALSE)
mod$format(canonicalize = TRUE)
#> data {
#> int N;
#> array[N] int y;
#> }
#> parameters {
#> real lambda;
#> }
#> model {
#> target += poisson_lpmf(y | lambda);
#> }
#>
#>
# }