Shiny应用本地正常但shinyapps.io部署失败及代码展示求助
解决Shiny部署报错与代码展示问题
咱们一步步解决你的两个问题:
一、修复shinyapps.io部署启动错误(exited with code 1)
你遇到的启动失败,核心问题大概率出在数据读取的路径或时机上,先明确几个关键规则:
- ❌ 绝对不能把
read.csv放在UI函数里:UI负责渲染前端界面,无法执行数据加载这类后端逻辑,会直接导致启动失败。 - ✅ 数据读取的正确位置:
- 全局环境(脚本顶层):如果数据量不大可以这么做,但必须保证部署时
matches.csv和你的Shiny脚本(比如app.R)在同一个文件夹,且部署时把这个文件一起上传到shinyapps.io。 - Server函数内部:这是更稳妥的做法,能避免部署时的路径歧义,还能利用Shiny的错误处理机制。
- 全局环境(脚本顶层):如果数据量不大可以这么做,但必须保证部署时
修改后的代码示例(数据读取移到Server内)
library(shiny) library(rsconnect) ui <- shinyUI(fluidPage( titlePanel("Game time by server"), sidebarLayout( sidebarPanel( selectInput("region", "Server:", choices = NULL), # 先留空,后面在server里动态赋值 hr(), selectInput(inputId = "n_breaks", label = "Number of bins in histogram (approximate):", choices = c(10, 20, 35, 50), selected = 20), hr(), checkboxInput(inputId = "individual_obs", label = strong("Show individual observations"), value = FALSE), checkboxInput(inputId = "density", label = strong("Show density estimate"), value = FALSE), conditionalPanel(condition = "input.density == true", sliderInput(inputId = "bw_adjust", label = "Bandwidth adjustment:", min = 0.2, max = 2, value = 1, step = 0.2)), hr(), helpText("Data from Kaggle (2014-2018) League of Legends Ranked Matches.") ), mainPanel( plotOutput("timePlot") ) ) )) server <- function(input, output, session) { # 把数据读取移到server内部,确保部署时路径正确 matches <- read.csv("matches.csv", stringsAsFactors=FALSE, sep=",", header=TRUE) matches <- matches[,c(3,6)] matches$platformid <- as.factor(matches$platformid) # 动态更新selectInput的选项(因为现在数据在server里,UI初始化时还没加载数据) updateSelectInput(session, "region", choices = levels(matches$platformid)) output$timePlot <- renderPlot({ hist(matches[matches$platformid==input$region,2], probability = TRUE, breaks = as.numeric(input$n_breaks), main = "Game Time", ylab="", xlab="Duration (seconds)") if (input$individual_obs) { rug(matches[matches$platformid==input$region,2]) } if (input$density) { dens <- density(matches[matches$platformid==input$region,2], adjust = input$bw_adjust) lines(dens, col = "blue") } }) } shinyApp(ui = ui, server = server)
部署注意事项
- 在RStudio部署时,一定要选择包含
app.R和matches.csv的整个文件夹,而不是只上传脚本文件。 - 去掉本地的
setwd语句(你已经做了),因为shinyapps.io会自动以应用所在目录为工作目录。
二、在应用中展示R代码
完全可以实现!给你两种实用方案:
方案1:用原生Shiny组件展示静态代码
用verbatimTextOutput渲染带格式的代码块,不用额外安装包:
# 修改UI部分,添加代码展示区域 ui <- shinyUI(fluidPage( titlePanel("Game time by server"), sidebarLayout( # ...原sidebarPanel内容不变... mainPanel( plotOutput("timePlot"), hr(), h3("应用源码"), verbatimTextOutput("appCode") ) ) )) # 在server里添加代码输出逻辑 server <- function(input, output, session) { # ...原数据读取和绘图逻辑不变... output$appCode <- renderPrint({ # 把你的完整代码作为字符串(注意转义内部的单引号) code_content <- ' library(shiny) library(rsconnect) ui <- shinyUI(fluidPage( titlePanel("Game time by server"), sidebarLayout( sidebarPanel( selectInput("region", "Server:", choices = NULL), hr(), selectInput(inputId = "n_breaks", label = "Number of bins in histogram (approximate):", choices = c(10, 20, 35, 50), selected = 20), hr(), checkboxInput(inputId = "individual_obs", label = strong("Show individual observations"), value = FALSE), checkboxInput(inputId = "density", label = strong("Show density estimate"), value = FALSE), conditionalPanel(condition = "input.density == true", sliderInput(inputId = "bw_adjust", label = "Bandwidth adjustment:", min = 0.2, max = 2, value = 1, step = 0.2)), hr(), helpText("Data from Kaggle (2014-2018) League of Legends Ranked Matches.") ), mainPanel( plotOutput("timePlot"), hr(), h3("应用源码"), verbatimTextOutput("appCode") ) ) )) server <- function(input, output, session) { matches <- read.csv("matches.csv", stringsAsFactors=FALSE, sep=",", header=TRUE) matches <- matches[,c(3,6)] matches$platformid <- as.factor(matches$platformid) updateSelectInput(session, "region", choices = levels(matches$platformid)) output$timePlot <- renderPlot({ hist(matches[matches$platformid==input$region,2], probability = TRUE, breaks = as.numeric(input$n_breaks), main = "Game Time", ylab="", xlab="Duration (seconds)") if (input$individual_obs) { rug(matches[matches$platformid==input$region,2]) } if (input$density) { dens <- density(matches[matches$platformid==input$region,2], adjust = input$bw_adjust) lines(dens, col = "blue") } }) output$appCode <- renderPrint({ cat(code_content) }) } shinyApp(ui = ui, server = server) ' cat(code_content) }) }
方案2:用shinyAce实现语法高亮
如果想要更专业的语法高亮效果,可以安装shinyAce包,支持R代码的颜色高亮、代码折叠:
# 先安装包 # install.packages("shinyAce") library(shinyAce) # 修改UI部分的代码展示区域 ui <- shinyUI(fluidPage( # ...原内容不变... mainPanel( plotOutput("timePlot"), hr(), h3("应用源码"), aceEditor( "appCode", mode = "r", # 指定语言为R theme = "github", # 选择主题 value = code_content,# 代码内容 readOnly = TRUE, # 设置为只读,防止用户修改 height = "600px" # 设置高度 ) ) ))
按照上面的修改,应该能解决部署报错,同时实现代码展示的需求。
内容的提问来源于stack exchange,提问作者Miles
相关产品推荐
相关产品推荐

