How to recompile the stan model during R package development

I’m working on an R package that uses Stan. I’m following the guidelines as best I can, but there’s one important detail that’s still unclear to me. When I edit a .stan file and want to have a clean slate, what’s the best way to guarantee it gets recompiled? I’m currently using this:

pkgbuild::compile_dll(path = ".", force = T)

But, the first command yields a confusing mix of 1) stale warnings that refer to lines that no longer exist in my .stan file and 2) intentional freshly added syntax errors that I used to check what .stan file is being used.

I think that is the way to do it within the devtools framework, but honestly I just do R CMD INSTALL ... in the Terminal window.

1 Like

I think I used to do devtools::clean_dll before compile_dll, but it’s been a while, so perhaps it won’t help.

I have used the Clean & Rebuild button in RStudio, which executes

R CMD INSTALL --preclean

(maybe with some more options that you can specify in preferences)

Thanks for all your solutions. It turns out my issue was a silly old copy of a file, which got parsed and compiled even though I was no longer calling the corresponding model from R. I’m still glad I asked; I think this thread could be informative for future readers.

Hi, Sorry to bring this up again. But I am not able to recompile stan files after editing them inside a R package. The use_rstan() command is failing to setup the file structures of the package. It is giving the following error message:
Error in if (a[k] > b[k]) return(1) else if (a[k] < b[k]) return(-1L) :
missing value where TRUE/FALSE needed
In addition: Warning message:
In utils::compareVersion(x, y) : NAs introduced by coercion

Please can you tell me what is the full work flow to recompile edited stan code?
I have tried R CMD INSTALL --preclean
That is just picking up the old version of the stan file and recompiles it – even when I deleted the stan file from inst/stan directory.
I have tried to start afresh by
and rstantools::use_rstan() command gives me the error messages. I think I am making a mistake in workflow somewhere.

I would like to tell stan that go back to day 0, (i.e. forget all the
compiled files), compile all the files now found in inst/stan directory and then build the shared library. But Stan wo’t listen to me!

Many thanks for your help.

Is it on some GitHub repo?

Thanks Ben. Sorry my package is not yet there in github!
But I think I solved the problem by running just rstan_config() instead of use_rstan() before re-building the package. The latter command gives the error message for existing packages with stan functionality.
I guess one line should be added to the page
In the advanced options section, it should say that everytime a stan file is changed the rstan_config() must be first issued before re-compiling the package. Otherwise, the older versions of stan files will be compiled.

Related to this, it should also say that if the stan files do not need to be recompiled then a command like devtools::install(’.’, quick=TRUE) should be issued to re-build the package.

Many thanks and best regards, Sujit

Can I hijack this and ask if there is a way to write my Makevars file so that if I’m making changes in only one of the stan files of a package with several stan files it only bothers to recompile the one I’m working on instead of all of them? Development in my package is sooo slow because of this. I usually just temporarily delete all the other files to speed things up.

I didn’t know about rstan_config() until now; so I’ve been deleting the relevant files in my src folder. If I change model1.stan then I go delete the previously compiled src/stanExports_model1.h, src/stanExports_model1.o and src/ Any models that I delete there in src/ will be recompiled, those that haven’t been deleted will not be rebuilt. I can’t answer your question exactly @mwmclean but sounds like this would be faster for you

I don’t know if that is possible for packages that were created with rstantools 2.x.

Looks like I created my package with rstantools v1.3.0 digging around the repo histories on GitHub. I don’t have equivalent files to to the ones you mention @cmcd, so I assume you’re using rstantools 2.x.

Should I be able set up the behaviour I want with rstantools 1.x @bgoodri? I basically was just following what you were doing with rstanarm at the time.

What we were doing at the time with rstanarm was adding an argument --precleanOOPS to the “Install and Restart” build options, which was sufficient to preclude RStudio from adding --preclean to the arguments but was unrecognized by R CMD INSTALL so it did not clean out the old object files every time. However, that meant when I wanted to do a clean rebuild, I had to do it outside of RStudio.