You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用if/else分支新增第三个ggplot图表运行报错排查

问题根因

你的代码报错核心是create_plot响应式表达式内的if/else条件分支语法不符合R语言规则:

  • R的链式if/else判断要求,else必须紧邻上一个分支的闭合花括号,且整个条件链只能存在1个放在最末尾的兜底else分支。
  • 你新增第三个图表时,在second分支后错误地先写了一个else块(黄线绘图逻辑),在这个块闭合后又追加了判断third的else if和第二个else块(棕线绘图逻辑),属于语法结构断裂,运行时会直接抛出意外的'else'符号错误,导致程序无法运行。
  • 额外冗余问题:你的下拉选择框仅配置了first/second/third3个选项,完全不需要两个多余的兜底else分支。
修正后可运行代码
library(shiny)
library(ggplot2)

# Define input choices
type <- c("first", "second","third")
# Data for lambda
table <- structure(list(year = 1991:2010, lambda = c(
  0.68854, 0.75545,
  1.63359, 1.22282, 1.70744, 1.09692, 0.51159, 1.3904, 1.09132,
  0.59846, 0.43055, 0.80135, 0.69027, 0.65646, 0.95485, 1.04818,
  0.67859, 1.00461, 1.16665, 1.28203
)), row.names = c(NA, -20L), class = "data.frame")


ui <- fluidPage(
  sidebarPanel(
    h3(""),
    
    # Dropdown to select the desired kind of graphic
    selectInput(
      inputId = "graphtype",
      label = "Graphic",
      choices = type,
      selected = "first"
    ),
    checkboxInput("Fixed", "Fixed Y axes", FALSE),
    downloadButton("downloadPlot", "Download Plot"),
    radioButtons(inputId = "type", label = "Select the file type", choices = list("png", "pdf"))
  ),
  # Graphic Area mainPanel. Graphic on top and table right below it
  mainPanel(
    plotOutput("plot"),
    dataTableOutput("mytable")
  )
)

######################
server <- function(input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })
  
  # Plot data
  create_plot <- reactive({
    xlabels <- 1991:2011
    if (input$graphtype == "first") {
      ggplot(table, aes(year, lambda)) +
        geom_line(size = 1.5, colour = "blue") +
        geom_point(colour = "orange", size = 4) +
        scale_x_continuous("", breaks = xlabels) +
        theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) +
        labs(x = "", y = "test", title = paste0("Population growth rate of Fish "))
    } else if (input$graphtype == "second") {
      ggplot(table, aes(year, lambda)) +
        geom_line(size = 1.5, colour = "red") +
        geom_point(colour = "green", size = 4) +
        scale_x_continuous("", breaks = xlabels) +
        theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) +
        labs(x = "", y = "fish test", title = paste0("Population growth"))
    } else if (input$graphtype == "third"){
      ggplot(table, aes(year, lambda)) +
        geom_line(size = 1.5, colour = "goldenrod") +
        geom_point(colour = "green", size = 4) +
        scale_x_continuous("", breaks = xlabels) +
        theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) +
        labs(x = "", y = "fish test", title = paste0("Population growth"))
    }
  })
  
  output$plot <- renderPlot({
    create_plot()
  })
  
  # 补全之前未实现的下载逻辑,直接调用响应式生成的当前图即可
  output$downloadPlot <- downloadHandler(
    filename = function() {
      paste0("plot_", Sys.Date(), ".", input$type)
    },
    content = function(file) {
      ggsave(file, plot = create_plot(), device = input$type)
    }
  )

}


shinyApp(ui = ui, server = server)
补充说明
  • 修正后三个选项对应三个绘图分支,切换下拉菜单即可正常展示三个不同配色的折线图
  • 代码补全了之前只放置按钮、未写实现的下载功能,点击即可按选择的格式下载当前展示的图表
  • 如果后续要新增更多图表,直接在现有else if后面追加新的分支判断即可,不要在分支链中间插入else块

内容的提问来源于stack exchange,提问作者Salvador

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 19:54:15