write_stan_file | R Documentation |
Convenience function for writing Stan code to a (possibly
temporary) file with a .stan
extension. By default, the
file name is chosen deterministically based on a hash
of the Stan code, and the file is not overwritten if it already has correct
contents. This means that calling this function multiple times with the same
Stan code will reuse the compiled model. This also however means that the
function is potentially not thread-safe. Using hash_salt = Sys.getpid()
should ensure thread-safety in the rare cases when it is needed.
write_stan_file(
code,
dir = getOption("cmdstanr_write_stan_file_dir", tempdir()),
basename = NULL,
force_overwrite = FALSE,
hash_salt = ""
)
code |
(character vector) The Stan code to write to the file. This can be a character vector of length one (a string) containing the entire Stan program or a character vector with each element containing one line of the Stan program. |
dir |
(string) An optional path to the directory where the file will be
written. If omitted, a global option |
basename |
(string) If |
force_overwrite |
(logical) If set to |
hash_salt |
(string) Text to add to the model code prior to hashing to
determine the file name if |
The path to the file.
# stan program as a single string
stan_program <- "
data {
int<lower=0> N;
array[N] int<lower=0,upper=1> y;
}
parameters {
real<lower=0,upper=1> theta;
}
model {
y ~ bernoulli(theta);
}
"
f <- write_stan_file(stan_program)
print(f)
lines <- readLines(f)
print(lines)
cat(lines, sep = "\n")
# stan program as character vector of lines
f2 <- write_stan_file(lines)
identical(readLines(f), readLines(f2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.