Compile Stan model in Shiny App?


I hope this is the place to ask this. Let me know if I need to go elsewhere! I’m trying to run rstan in a shiny app I’m creating. It runs fine locally, but then when I deploy it to, I seem unable to compile the model. Thing’s I’ve tried:

  1. Compile the model in global.R using stan_model(); this gives me the following error when I try to deploy to (the app doesn’t work at all in this case):

    Warning in system(cmd, intern = !verbose) :
    running command ‘/opt/R/3.5.2/lib/R/bin/R CMD SHLIB file19349976b7.cpp 2> file19349976b7.cpp.err.txt’ had status 1
    Error in value[3L] : invalid connection
    Calls: local … tryCatch -> tryCatchList -> tryCatchOne ->
    Execution halted

  2. Save a locally compiled stanmodel and load it into the shiny environment in the app. The app runs but it is unable to use the pre-compiled stanmodel object. I understand that this is probably because you can’t use compiled stanmodel objects from a different computer.

  3. Include a button in the app that will trigger the stan_model() function. This seems to work for a bit, but then the app disconnects/times out because it takes too long.

I hope that anyone can help me with my problem. Let me know if I need to provide any additional information.

I recall someone getting this to work awhile ago; maybe @denne ? I think the real answer is that you should create a small R package that has your model precompiled following

but with the GitHub version of the rstantools package

Then presumably will let you load that package an draw from the posterior distribution.

It still works on travisCI, but some of the comments in the readme might no longer be valid.

To compile a Stan model on you need to use the xxlarge instance with 4 gb ram.

Thanks for sharing this idea! I’m going to look into it… Not really experienced with creating R packages but the links you’ve shared should hopefully help.

Thanks for sharing this! I’m going to try and make it work and I’ll report back here.

when I implement Rstan in Shiny, then the following error occurred, and I guess the error message Out of memory relates your suggestion, but I do not know how to use 4Gb ram. Is it free? How to extend the memory?

I found, I should pay money to upload rstan with Shiny.
Unfortunately, I will give up. :’-D

2019-08-06T08:14:13.288608+00:00 shinyapps[system]: Out of memory!
2019-08-06T08:14:13.302116+00:00 shinyapps[1067050]: Warning in system(cmd, intern = !verbose) :
2019-08-06T08:14:13.302119+00:00 shinyapps[1067050]:   running command '/opt/R/3.6.1/lib/R/bin/R CMD SHLIB file1a3c160a9.cpp 2> file1a3c160a9.cpp.err.txt' had status 1
2019-08-06T08:14:13.378902+00:00 shinyapps[1067050]:   133: sink
2019-08-06T08:14:13.378904+00:00 shinyapps[1067050]:   132: cxxfunctionplus
2019-08-06T08:14:13.378905+00:00 shinyapps[1067050]:   131: rstan::stan_model

Yes, it seems that the free and starter plans are limited to 1 GB. So compiling on requires you to have at minimum the basic plan.

If it possible for you to use a pre-compiled model then I think that’s the way to go. I think shinyapps uses AWS instances with Ubuntu based OS. If you are a mac or windows user, then following approach should work: start an instance on AWS, with for example these AMI (or use Docker on your own computer). Compile the model there and then use it for you Shiny app. I’ve not actually tested it myself, but should work.

Even if , the following very simple Shiny code with Rstan, the error occurred as follows;

Thus I guess upload Shiny codes including Rstan is impossible in usual ways. (Or the following code is something wrong?)

I knew AWS and AMI or Docker for the first time, so now I cannot understand your suggestion, or what should I do. Please let me know if you have any idea.

2019-08-07T06:47:30.127423+00:00 shinyapps[1072302]: 180: rstan::stan_model
2019-08-07T06:47:30.127424+00:00 shinyapps[1072302]: 179: renderPlot [/srv/connect/apps/aaaff/app.R#42]
2019-08-07T06:47:30.127424+00:00 shinyapps[1072302]: 177: func
2019-08-07T06:47:30.127425+00:00 shinyapps[1072302]: 137: drawPlot
2019-08-07T06:47:30.127426+00:00 shinyapps[1072302]: 107: drawReactive
2019-08-07T06:47:30.127426+00:00 shinyapps[1072302]: 94: origRenderFunc
2019-08-07T06:47:30.127427+00:00 shinyapps[1072302]: 93: output$distPlot
2019-08-07T06:47:30.127425+00:00 shinyapps[1072302]: 123: reactive:plotObj
2019-08-07T06:47:30.127428+00:00 shinyapps[1072302]: 12: fn
2019-08-07T06:47:30.127427+00:00 shinyapps[1072302]: 13: runApp
2019-08-07T06:47:30.127428+00:00 shinyapps[1072302]: 7: connect$retry
2019-08-07T06:47:30.127429+00:00 shinyapps[1072302]: 6: eval
2019-08-07T06:47:30.127429+00:00 shinyapps[1072302]: 5: eval
2019-08-07T06:50:30.877478+00:00 shinyapps[system]: Out of memory!
2019-08-07T06:50:30.921102+00:00 shinyapps[1072302]: 137: drawPlot
2019-08-07T06:50:30.882856+00:00 shinyapps[1072302]: Warning in system(cmd, intern = !verbose) :
2019-08-07T06:50:30.882859+00:00 shinyapps[1072302]: running command ‘/opt/R/3.6.1/lib/R/bin/R CMD SHLIB file17f6d2353.cpp 2> file17f6d2353.cpp.err.txt’ had status 1
2019-08-07T06:50:30.914235+00:00 shinyapps[1072302]: Warning: Error in sink: invalid connection
2019-08-07T06:50:30.921098+00:00 shinyapps[1072302]: 182: sink
2019-08-07T06:50:30.921100+00:00 shinyapps[1072302]: 180: rstan::stan_model
2019-08-07T06:50:30.921101+00:00 shinyapps[1072302]: 179: renderPlot [/srv/connect/apps/aaaff/app.R#42]
2019-08-07T06:50:30.921099+00:00 shinyapps[1072302]: 181: cxxfunctionplus
2019-08-07T06:50:30.921101+00:00 shinyapps[1072302]: 177: func
2019-08-07T06:50:30.921103+00:00 shinyapps[1072302]: 107: drawReactive
2019-08-07T06:50:30.921103+00:00 shinyapps[1072302]: 94: origRenderFunc
2019-08-07T06:50:30.921104+00:00 shinyapps[1072302]: 93: output$distPlot
2019-08-07T06:50:30.921104+00:00 shinyapps[1072302]: 13: runApp
2019-08-07T06:50:30.921105+00:00 shinyapps[1072302]: 12: fn
2019-08-07T06:50:30.921105+00:00 shinyapps[1072302]: 7: connect$retry
2019-08-07T06:50:30.921140+00:00 shinyapps[1072302]: 5: eval
2019-08-07T06:50:30.921102+00:00 shinyapps[1072302]: 123: reactive:plotObj
2019-08-07T06:50:30.921105+00:00 shinyapps[1072302]: 6: eval
2019-08-07T06:51:00.989733+00:00 shinyapps[system]: Out of memory!
2019-08-07T06:51:01.033105+00:00 shinyapps[1072302]: 137: drawPlot
2019-08-07T06:51:01.033106+00:00 shinyapps[1072302]: 107: drawReactive
2019-08-07T06:51:01.033106+00:00 shinyapps[1072302]: 94: origRenderFunc
2019-08-07T06:51:00.995083+00:00 shinyapps[1072302]: Warning in system(cmd, intern = !verbose) :
2019-08-07T06:51:01.033107+00:00 shinyapps[1072302]: 93: output$distPlot
2019-08-07T06:51:00.995094+00:00 shinyapps[1072302]: running command ‘/opt/R/3.6.1/lib/R/bin/R CMD SHLIB file172b8bf365.cpp 2> file172b8bf365.cpp.err.txt’ had status 1
2019-08-07T06:51:01.033107+00:00 shinyapps[1072302]: 13: runApp
2019-08-07T06:51:01.033091+00:00 shinyapps[1072302]: 182: sink
2019-08-07T06:51:01.033108+00:00 shinyapps[1072302]: 7: connect$retry
2019-08-07T06:51:01.026390+00:00 shinyapps[1072302]: Warning: Error in sink: invalid connection
2019-08-07T06:51:01.033108+00:00 shinyapps[1072302]: 12: fn
2019-08-07T06:51:01.033093+00:00 shinyapps[1072302]: 181: cxxfunctionplus
2019-08-07T06:51:01.033109+00:00 shinyapps[1072302]: 6: eval
2019-08-07T06:51:01.033103+00:00 shinyapps[1072302]: 180: rstan::stan_model
2019-08-07T06:51:01.033109+00:00 shinyapps[1072302]: 5: eval
2019-08-07T06:51:01.033104+00:00 shinyapps[1072302]: 179: renderPlot [/srv/connect/apps/aaaff/app.R#42]
2019-08-07T06:51:01.033104+00:00 shinyapps[1072302]: 177: func
2019-08-07T06:51:01.033105+00:00 shinyapps[1072302]: 123: reactive:plotObj

Shiny code with Rstan


# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)

        # Show a plot of the generated distribution

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        model <- rstan::stan_model(
            model_code = "parameters {real y;} model {y ~ normal(0,1);}            "
        fit <- rstan::sampling(model)
        # draw the histogram with the specified number of bins
        # hist(x, breaks = bins, col = 'darkgray', border = 'white')

# Run the application 
shinyApp(ui = ui, server = server)