Combining write_stan_file with include from temp.dir()

Is there a way to include functions written with write_stan_file() using cmdstanr? Specifically when written to dir = tempdir()?

Say I wrote a stan file called stan_funcs_file using write_stan_file() as below:

stan_funcs <- "functions { }"
stan_funcs_file <- write_stan_file(stan_funcs)

Is there any way to call it as an include from another stan file using cmdstanr_model from the default dir = tempdir()? I am confused about the appropriate syntax.

I also have a problem with how I write the include in the main model code.

When I try to add the include # include stan_funcs_file.stan I get the error Comments beginning with # are deprecated.

1 Like

write_stan_file is intended for examples and quick hacks, so it is quite possible that your are hitting the limits of what it can do. If you need includes, I would strongly suggest you store your models in files in your project and not rely on write_stan_file. If you really need to write the file programatically (e.g. because it is generated code), I would suggest you write the temporary file yourself, to keep control over the naming and location.

Shouldn’t that be #include (without the space)?

Best of luck with your model!

2 Likes