Check syntax of a Stan program — model-method-check_syntax • cmdstanr
Description: The $check_syntax() method of a CmdStanModel object checks the Stan program for syntax errors and returns TRUE (invisibly) if parsing succeeds. If invalid syntax in found an error is thrown.

# Check syntax of a Stan program

Source: `R/model.R`
`model-method-check_syntax.Rd`

The `$check_syntax()` method of a `CmdStanModel` object checks the Stan program for syntax errors and returns `TRUE` (invisibly) if parsing succeeds. If invalid syntax in found an error is thrown.

    check_syntax(
      pedantic = FALSE,
      include_paths = NULL,
      stanc_options = list(),
      quiet = FALSE
    )

## Arguments

pedantic

  (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.

include_paths

  (character vector) Paths to directories where Stan should look for files specified in `#include` directives in the Stan program.

stanc_options

  (list) Any other Stan-to-C++ transpiler options to be used when compiling the model. See the documentation for the `$compile()` method for details.

quiet

  (logical) Should informational messages be suppressed? The default is `FALSE`, which will print a message if the Stan program is valid or the compiler error message if there are syntax errors. If `TRUE`, only the error message will be printed.

## Value

The `$check_syntax()` method returns `TRUE` (invisibly) if the model is valid.

## See also

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-compile`, `model-method-diagnose`, `model-method-expose_functions`, `model-method-format`, `model-method-generate-quantities`, `model-method-optimize`, `model-method-sample_mpi`, `model-method-sample`, `model-method-variables`, `model-method-variational`

## Examples

    # \dontrun{
    file <- write_stan_file("
    data {
      int N;
      int y[N];
    }
    parameters {
      // should have <lower=0> but omitting to demonstrate pedantic mode
      real lambda;
    }
    model {
      y ~ poisson(lambda);
    }
    ")
    mod <- cmdstan_model(file, compile = FALSE)

    # the program is syntactically correct, however...
    mod$check_syntax()
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 4, column 2: Declaration
    #>     of arrays by placing brackets after a variable name is deprecated and
    #>     will be removed in Stan 2.33.0. Instead use the array keyword before the
    #>     type. This can be changed automatically using the auto-format flag to
    #>     stanc
    #> Stan program is syntactically correct

    # pedantic mode will warn that lambda should be constrained to be positive
    # and that lambda has no prior distribution
    mod$check_syntax(pedantic = TRUE)
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 4, column 2: Declaration
    #>     of arrays by placing brackets after a variable name is deprecated and
    #>     will be removed in Stan 2.33.0. Instead use the array keyword before the
    #>     type. This can be changed automatically using the auto-format flag to
    #>     stanc
    #> Warning in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpFBtN6X/model_74b584fd23ea8c78eceda86d1d425fe8.stan', line 11, column 14: A
    #>     poisson distribution is given parameter lambda as a rate parameter
    #>     (argument 1), but lambda was not constrained to be strictly positive.
    #> Warning: The parameter lambda has no priors. This means either no prior is
    #>     provided, or the prior(s) depend on data variables. In the later case,
    #>     this may be a false positive.
    #> Stan program is syntactically correct
    # }