r-为什么Shiny在尝试使用rstanarm包部署应用程序时出错



我已经在Shiny服务器站点上的Shiny应用程序中使用rstanarm包很长时间了,并且已经编写了许多应用程序。最近,Shiny在尝试上传使用rstanarm的新应用程序时出错。如果我不使用rstanarm,没有问题。带有rstanarm的Shiny应用程序在本地运行良好,只是不会构建到Shiny。以下是部署日志的代码和结尾:

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

当工作中的IT小组用最新的Microsoft补丁更新了我的Windows 10电脑时,问题就开始了。这可能是巧合,我所知道的是,在这之前,我能够使用rstanarm将应用程序部署到Shiny服务器上。我还使用R、RStudio、rtools和所有相关软件包的最新下载在非工作计算机上进行了尝试,但我得到了相同的错误。但这并不排除它是Windows的东西,因为非工作计算机也有所有最新的Windows更新。

版本:Windows 10,R 4.0.5,RStudio 1.4.1106,rstanarm 2.21.1,rstan 2.21.2,shiny 1.6.0,shiny Widgets 0.6.0,rtools 4.0,Rcpp 1.0.6,RcpEigen 0.3.3.9.1,rsconnect 0.8.17。

到目前为止我尝试过的:

  • 在截至2021年4月初从未安装过R/RStudio的Windows 10计算机上安装所有最新的软件
  • 当部署到Shiny时,回滚到早期的R版本可以很好地使用rstanarm,例如4.0.3(我认为(和3.6.3
  • 从源代码而不是编译版本安装rstanarmrstan
  • 编写一个新的、非常简单的应用程序,无论是否使用rstanarm,在使用rstanarm时都会出现相同的错误

还有一件奇怪的事情刚刚开始——当我在Shiny应用程序中调用r会话时,rstan会崩溃,即使是在本地运行。Rstan在RStudio脚本中运行时不会使R崩溃,但在Shiny应用程序中运行时会使其崩溃。与rstanarm不同,带有Rstan的应用程序将构建到Shiny服务器,但Shiny服务器上的Rstan应用程序在运行时出错,可能是因为它崩溃了R.

如果我在错误的地方发布了这个问题,请原谅我,并将我重定向到合适的地方发布这样的问题。

谢谢。

我一开始写这篇文章是作为评论,但内容太多了。关于这个问题,我一直在考虑我能想到的任何事情。如果没有你的数据,我就无法运行该文件,看看我是否遇到了同样的错误。出于好奇,您是否查看了rstanarm包及其依赖项上次更新的时间?你知道这个应用程序什么时候确实有效吗?它在12月起作用了吗,但现在不行了?诸如此类的事情。

要查看依赖项,请使用tools::package_dependencies("rstanarm")

我查看了我设备上软件包的更新,有几个是最近更新的。如果我们能把它缩小到一揽子问题,那将使解决问题变得容易得多。

如果你想查看依赖项和包上次更新的日期,请使用以下内容:

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 

或者,你可以用这种方式查看设备上的内容,但它只显示版本,而不显示日期。

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

这就是我的设备所在的位置。包中的更改或Excel的更新可能是问题所在。尽管我确实花了一些时间来浏览你使用过的功能,以及它们是如何与任何更新联系在一起的,但我没有任何东西跳出来

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

我会继续查看,看看你是否在这里提供了更多信息。如果没有其他东西,如果数据不是专有的,那么数据或数据的最小可复制示例-excel电子表格以及无论你如何设置它们,都会让这件事变得更容易。

LPA,

这很奇怪,因为即使只是使用默认的Shiny示例并包括库rstanarm,我也会收到同样的错误。我已经在多台运行Windows 10的机器上试用了它,并进行了最新的R和RStudio更新。代码在本地工作,但无法发布。

虽然这绝对不是一个答案,但我希望其他人可能能够使用以下简单的代码来帮助进行故障排除。

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)

最新更新