# Compile Stan model in Shiny App?

**URL:** <https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022>\
**Category:** RStan\
**Created:** [August 2, 2019, 12:14am UTC](https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022 "2019-08-02T00:14:48Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [August 2, 2020, 3:25pm UTC](https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022/21 "2020-08-02T15:25:18Z")

</div>

As I have a windows system, I set myself up on the amazon web service and set up RStudio as is described on [http://www.louisaslett.com/RStudio\_AMI](http://www.louisaslett.com/RStudio_AMI). But, I can’t get Stan to run at all on there.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/8/8ee776a3a0b4d4be3d218a09cc846483c81ed2eb.png)

Any idea why I can’t compile a Stan model on there? I’m trying to use a compiled model to make a shiny app as I don’t think my compiled model on windows seems to be working for it: [https://vianey-leosbarajas.shinyapps.io/BayesSIR/](https://vianey-leosbarajas.shinyapps.io/BayesSIR/)  
@charlesm93

---

<div class="post-metadata">

**Author:** ![CerulloE](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cerulloe/32/15968_2.png) [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Post date:** [July 2, 2022, 1:11pm UTC](https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022/22 "2022-07-02T13:11:16Z")

</div>

Sorry if this is a silly question, but once all of these steps are complete and the app is deployed from a Linux system using the .rds files for the models with the correct compiler flag, will the app work on [shinyapps.io](http://shinyapps.io) from any computer? i.e. will the person using the app need to follow these steps every time, or will the app “just work” by using the compiled .rds files?

---

<div class="post-metadata">

**Author:** ![andrewGhazi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewghazi/32/18855_2.png) [@andrewGhazi](https://discourse.mc-stan.org/u/andrewGhazi)\
**Post date:** [June 23, 2026, 11:51pm UTC](https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022/23 "2026-06-23T23:51:59Z")

</div>

I encountered this task recently and just wanted to share some additional details I worked out for generating a shinyapps-compatible binary inside a container.

One can run the [rocker r2u image](https://rocker-project.org/images/#additional-images) with the Ubuntu version matching [the shinyapps infrastructure](https://docs.posit.co/shinyapps.io/guide/appendix) (22.04 at time of writing) using Podman or Docker. r2u provides pre-compiled binaries of R packages for Ubuntu, so installing rstan inside the container only takes ~15 seconds. These are the steps:

```bash
# Start the container, mapping the host path with your .stan file to a folder in the container:
podman run -it -v /host/path/to/stan/:/home/rocker/stan docker.io/rocker/r2u:22.04

# Inside the container, install rstan:
Rscript -e "install.packages('rstan')"

# Update the compiler flags with sed:
sed -i 's/^CXXFLAGS.*/CXXFLAGS = -g -O2 $(LTO)/' /usr/lib/R/etc/Makeconf

# And compile + save your model:
Rscript -e "library(rstan); m = stan_model('/home/rocker/stan/model.stan'); saveRDS(m, '/home/rocker/stan/model.rds')"

```

And the RDS will now be in the host folder you mapped.

I’m not positive that shinyapps and the corresponding r2u image will always be using the same R version, so you may need to update R in the latter to match the former. But at time of writing they’re both on 4.6.0 and everything works with no tweaks :)

[Previous page](https://discourse.mc-stan.org/t/compile-stan-model-in-shiny-app/10022.md?page=1)
