2

Have been using the rstanarm package in Shiny apps on the Shiny server site for a long time and with a number of apps I've written. Recently, Shiny gives an error when trying to upload a new app that uses rstanarm. No problems if I do not use rstanarm. The Shiny app with rstanarm runs fine locally, it just won't build to Shiny. Below is the code and the end of the deployment log:

BEGINNING OF CODE
library(shiny)
library(shinyWidgets)
library(rstanarm)
library(readxl)
library(tidyverse)

ui <- fluidPage(

    titlePanel("Check AV22"),

    sidebarLayout(
        sidebarPanel(
            fileInput('path', 'Choose file to upload',
                      accept = c(
                          'application/vnd.ms-excel',
                          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                          'application/vnd.ms-excel.sheet.macroEnabled.12'
                      )
            ),
        ),

        mainPanel(
           plotOutput("Plot")
        )
    )
)

server <- function(input, output) {

    df <- eventReactive(input$path,{ 
        inFile <- input$path
        if(is.null(inFile))
            return(NULL)
        read_excel("./grbg.xlsx",sheet=2,skip=3)
    })
    
    prior <- reactive({
        prior <- read.csv("./coefficients.csv")[20,] 
    })

    stan_data <- reactive({
        list(N=nrow(df()),x=df()$conc,y=df()$ds,mnint=prior()$mean_intercept,
             sdint=prior()$sd_intercept,mnslope=prior()$mean_slope,
             sdslope=prior()$sd_slope)
    })
    
    post <- reactive({
        as.matrix(stan_glm(ds~conc,
                           data=df(),
                           family="binomial",
                           prior_intercept=normal(prior()$mean_intercept,
                                                  prior()$sd_intercept,
                                                  autoscale=TRUE),
                           prior=normal(prior()$mean_slope,
                                        prior()$sd_slope,
                                        autoscale=TRUE)))
    })
    
    q <- reactive({
        pred <- matrix(rep(NA,4000*1000),nrow=4000)
        x <- seq(0,max(df()$conc),length=1000)
        for(i in 1:1000) {
            pred[,i] <- 1 / (1 + exp(-(post()[,1] + post()[,2]*x[i])))
        }
        pred_quantiles <- t(apply(pred,2,function(x) quantile(x,c(.1,.5,.8))))
        pred_quantiles <- data.frame(x,pred_quantiles)
        names(pred_quantiles) <- c("x","q1","q5","q8")
        
        pd90 <- (log(0.9/0.1) - post()[,1])/post()[,2]
        pd90_quantiles <- quantile(pd90,c(0.1,0.5,0.8))
        list(pred_quantiles=pred_quantiles,pd90_quantiles=pd90_quantiles)
    })
    
    output$Plot <- renderPlot({
         ggplot() + geom_point(data=df(),aes(x=conc,y=ds),shape=4,col="black",size=2) +
            geom_line(data=q()$pred_quantiles,aes(x=x,y=q1),col="slategray3",size=1) +
            geom_line(data=q()$pred_quantiles,aes(x=x,y=q5),col="mediumblue",size=1) +
            geom_line(data=q()$pred_quantiles,aes(x=x,y=q8),col="violet",size=1) +
            geom_vline(xintercept=q()$pd90_quantiles[2],linetype="dashed") +
            theme_classic()
    })

}

shinyApp(ui = ui, server = server)
ENDING OF CODE

FINAL PART OF DEPLOYMENT LOG SHOWING ERROR
Eigen::Product<Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const  
Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const  
Eigen::Matrix<double, 1, -1> >, const Eigen::Transpose<Eigen::Matrix<double, -1, 1> > >,    
Eigen::Matrix<double, -1, -1>, 0>; Rhs = Eigen::Matrix<double, -1, 1>]’
    /opt/R/4.0.4/lib/R/library/RcppEigen/include
################################# End Task Log #################################  
Error: Unhandled Exception: Child Task 905587151 failed: Error building image:   
Error building rstanarm (2.21.1). Build exited with non-zero status: 1  
Execution halted

The problem started when the IT group at work updated my Windows 10 computer with the latest Microsoft patches. That could be a coincidence, all I know is that I was able to deploy apps with rstanarm to the Shiny server up until this time. I've also tried it out on a non-work computer using the latest downloads of R, RStudio, rtools and all the associated packages, and I get the same error. But that doesn't exclude it being a Windows thing because the non-work computer also has all the latest Windows updates.

Versions: Windows 10, R 4.0.5, RStudio 1.4.1106, rstanarm 2.21.1, rstan 2.21.2, shiny 1.6.0, shinyWidgets 0.6.0, rtools 4.0, Rcpp 1.0.6, RcppEigen 0.3.3.9.1, rsconnect 0.8.17.

What I've tried so far:

  • Install all the latest of everything on a Windows 10 computer that has never had R/RStudio as of early April 2021.
  • Roll back to earlier R versions when deploying to Shiny was working fine with rstanarm such as 4.0.3 (I think) and definitely 3.6.3.
  • Install rstanarm and rstan from source instead of the compiled versions.
  • Write a new, very simple app, both with and without rstanarm, and get the same error when using rstanarm.

One other weird thing that has also just started - rstan crashes the r session when I call it from within a Shiny app, even running locally. Rstan does not crash R when run in an RStudio script, but it does crash it when run in a Shiny app. The app with Rstan will build to the Shiny server, unlike rstanarm, but then the rstan app on the Shiny server errors out when run, presumably because it crashes R.

Please forgive me if I've posted this in the wrong place, and redirect me to the proper place to post such a question.

Thank you.

LPA
  • 21
  • 3
  • If you add the coding you're using I think you'll be more likely to get the help you're looking for. – Kat Apr 11 '21 at 04:41
  • Okay, thank you, I've added the code into the original post. – LPA Apr 11 '21 at 17:10
  • PS I just tried adding library(rstanarm) to the top of the Shiny app template in RStudio, the one that shows a histogram and sliderInput, and got the same error. – LPA Apr 11 '21 at 19:32

2 Answers2

0

I started out writing this as a comment, but it was too much. I've been going through anything I can think of regarding this issue. I can't run the file without your data to see if I run into the same error. Out of curiosity, have you looked at when the rstanarm package and its dependencies were last updated? Do you know when the app definitely worked? Did it work in December, but not now? That sort of thing.

To see the dependencies use tools::package_dependencies("rstanarm")

I looked at the updates of the packages on my device and there are several that were updated very recently. If we can narrow it down to a package issue, that will make solving the problem a lot easier.

If you want to see the dependencies and the dates the packages were last updated use this:

library(tidyverse) # for dplyr
library(lubridate) # for as_date()

pk <- package_dependencies("rstanarm") %>% unlist()
data.frame("Packages" = pk,
           "Date_updated" = lapply(pk, packageDate) %>% 
               unlist() %>% 
               as_date()
          ) # end data.frame

# 1          Rcpp 2021-01-14 
# 2       methods 2020-10-11 
# 3     bayesplot 2021-01-07 
# 4       ggplot2 2020-12-17
# 5          lme4 2020-11-30
# 6           loo 2020-12-07 
# 7        Matrix 2019-11-25
# 8          nlme 2020-08-21
# 9         rstan 2020-07-07
# 10   rstantools 2020-07-05
# 11    shinystan 2018-04-29
# 12        stats 2020-10-11
# 13     survival 2020-09-24
# 14 RcppParallel 2021-02-24 
# 15        utils 2020-10-11
# 16  StanHeaders 2020-12-16
# 17           BH 2020-12-12
# 18    RcppEigen 2020-12-17 

Alternatively, you can look at what's on your device this way, but it only shows versions, not dates.

ps <- packageStatus()
data.frame(Package = ps$inst$Package,
           V = ps$inst$Version, stat = ps$inst$Status) %>%
    filter(Package %in% pk) %>%
    left_join(ps$avail[1:2]) 

This is where my device is at. A change in the package or an update to Excel could possibly be the issue. Although I did spend time going through the functions you've used and how they tie into any of the updates and nothing jumped out at me.

#         Package         V    stat   Version
# 1     bayesplot     1.8.0      ok     1.8.0
# 2            BH  1.75.0-0      ok  1.75.0-0
# 3       ggplot2     3.3.3      ok     3.3.3
# 4          lme4    1.1-26      ok    1.1-26
# 5           loo     2.4.1      ok     2.4.1
# 6        Matrix    1.2-18 upgrade     1.3-2
# 7       methods     4.0.3      ok      <NA>
# 8          nlme   3.1-149 upgrade   3.1-152
# 9          Rcpp     1.0.6      ok     1.0.6
# 10    RcppEigen 0.3.3.9.1      ok 0.3.3.9.1
# 11 RcppParallel     5.0.3 upgrade     5.1.1
# 12        rstan    2.21.1 upgrade    2.21.2
# 13   rstantools     2.1.1      ok     2.1.1
# 14    shinystan     2.5.0      ok     2.5.0
# 15  StanHeaders  2.21.0-7      ok  2.21.0-7
# 16        stats     4.0.3      ok      <NA>
# 17     survival     3.2-7 upgrade    3.2-10
# 18        utils     4.0.3      ok      <NA>

I'll try to keep checking back, to see if you've put more information on here. If nothing else, if the data isn't proprietary, the data, or a minimal reproducible example of the data - the excel spreadsheets and however you've got them setup would make this a bit easier to help with, as well.

Kat
  • 15,669
  • 3
  • 18
  • 51
0

LPA,

This is curious as I too am getting the same error even just using the default Shiny example and including the library rstanarm. I've tried it on multiple machines running Windows 10 with the latest R and RStudio updates. The code works locally, but cannot be published.

While this is definitely not an answer I hope that perhaps others may be able to use the following simple code to help troubleshoot.

library(shiny)
library(rstanarm)

# 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 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# 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)

        # 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)
Scott Hunter
  • 254
  • 2
  • 14