Shiny应用部署至shinyapps.io时无法读取.RData文件的问题及解决方法
我有一个Shiny应用,本地运行完全正常,但部署到shinyapps.io后就报错了。日志里的错误信息是:
cannot open compressed file 'C:/Users/User/Documents/Ant/foo_upwork.RData', probable reason 'No such file or directory' Error in value[3L] : cannot open the connection
我确认本地的文件路径是对的,文件也确实存在,请问问题出在哪?怎么在shinyapps.io里正确读取.RData文件?
附带的应用代码如下:
library(shiny) library(ggplot2) load("C:/Users/User/Documents/Ant/foo_upwork.RData") ui <- pageWithSidebar( headerPanel("Scattetrplot"), sidebarPanel( selectInput("xcol", "X Variable", choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"), selected = "Acled_Total", multiple = F ), selectInput("ycol", "Y Variable", choices = c("I3", "I4", "I5", "I6"), selected = "I3", multiple = F ), ), mainPanel( plotOutput("plot1") ) ) server <- function(input, output) { new <- reactive({ myvars <- input$xcol newdata <- foo_upwork[myvars] }) output$plot1 <- renderPlot({ ggplot(foo_upwork, aes(x = .data[[input$xcol]], y = .data[[input$ycol]], fill = ADM0_FR, shape = ADM0_FR)) + geom_point(position = position_jitter(h = 0.1, w = 0.1), alpha = 0.5, size = 1.5) + scale_fill_manual(values = c("red", "blue", "orange")) + scale_shape_manual(values = c(23, 24, 25)) + geom_text( position = position_jitter(h = 0.10, w = 0.55), aes(label = ifelse(new()[, 1] > quantile(new()[, 1], 0.98), as.character(ADM3_FR), "" )), vjust = 3, size = 2.75 ) + labs(x = as.character(input$xcol), y = as.character(input$ycol)) + theme_bw() }) } shinyApp(ui, server)
这个问题很典型——你混淆了本地Windows文件系统的绝对路径和shinyapps.io云端服务器的文件系统!
问题根源
shinyapps.io是运行在云端的Linux服务器,它根本没有你本地的C:/Users/User/Documents/Ant/这个路径,自然找不到你的.RData文件。本地的绝对路径只在你自己的电脑上有效,部署到云端后完全失效。
解决步骤
整理项目文件结构
把foo_upwork.RData文件移动到你的Shiny项目的根目录下——也就是和你的这个app脚本(比如app.R)放在同一个文件夹里。修改
load()的路径为相对路径
把代码里的第3行从绝对路径改成相对路径:# 原来的无效代码 # load("C:/Users/User/Documents/Ant/foo_upwork.RData") # 修改后的有效代码 load("foo_upwork.RData")确保部署时包含该文件
用rsconnect部署应用的时候,默认会上传项目目录里的所有文件,只要你的.RData文件在项目根目录,就会被一起传到shinyapps.io服务器上。如果之前排除过文件,记得检查部署配置,确保没漏掉这个.RData。
额外验证小技巧
在本地运行修改后的代码前,可以先执行file.exists("foo_upwork.RData"),如果返回TRUE,说明相对路径是正确的,部署后就不会出问题了。
修改后的完整代码(重点改了第3行):
library(shiny) library(ggplot2) load("foo_upwork.RData") # 这里改成了相对路径 ui <- pageWithSidebar( headerPanel("Scattetrplot"), sidebarPanel( selectInput("xcol", "X Variable", choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"), selected = "Acled_Total", multiple = F ), selectInput("ycol", "Y Variable", choices = c("I3", "I4", "I5", "I6"), selected = "I3", multiple = F ), ), mainPanel( plotOutput("plot1") ) ) server <- function(input, output) { new <- reactive({ myvars <- input$xcol newdata <- foo_upwork[myvars] }) output$plot1 <- renderPlot({ ggplot(foo_upwork, aes(x = .data[[input$xcol]], y = .data[[input$ycol]], fill = ADM0_FR, shape = ADM0_FR)) + geom_point(position = position_jitter(h = 0.1, w = 0.1), alpha = 0.5, size = 1.5) + scale_fill_manual(values = c("red", "blue", "orange")) + scale_shape_manual(values = c(23, 24, 25)) + geom_text( position = position_jitter(h = 0.10, w = 0.55), aes(label = ifelse(new()[, 1] > quantile(new()[, 1], 0.98), as.character(ADM3_FR), "" )), vjust = 3, size = 2.75 ) + labs(x = as.character(input$xcol), y = as.character(input$ycol)) + theme_bw() }) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者firmo23

