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

Shiny应用navbarPage仅单标签页可显示绘图的问题排查求助

Shiny应用问题修复方案

核心错误点

  • 输入ID全局重复:两个tab页的下拉选择框都使用了variable作为inputId,Shiny要求所有输入组件ID必须全局唯一,导致第二个页的选择参数无法正常传递。
  • 输出定义嵌套:output$info2的定义代码被写在了output$plot2的renderPlot函数内部,导致renderPlot没有返回ggplot绘图对象,无法渲染出图表。
  • 缺失依赖包加载:代码中使用了lubridate包的ymd、months函数,但没有提前加载该包会触发运行报错。

修复后完整代码

library(shiny)
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(shinythemes)
library(lubridate) # 补上缺失的依赖包

dates <- ymd("2016-01-01")+ months(0:59)
fake_values <- 
  c(661,678,1094,1987,3310,2105,1452,983,1107)

replicate <- rep(1,60) %*% t.default(fake_values)
replicate <- as.data.frame(replicate)

df <- bind_cols(replicate, dates) %>%
  rename(dates = ...10)

## 数据长宽转换
data <- reshape2::melt(df, id.var='dates')
data$variable <- as.character(data$variable)
data$dates <- as.Date(data$dates)
data2 <- reshape2::melt(df, id.var='dates')
data2$variable <- as.character(data2$variable)
data2$dates <- as.Date(data$dates)


#### UI部分
ui <- 
  navbarPage(
    
    title="Zeitreihenvorhersage",
    theme=shinytheme("spacelab"),
    inverse=TRUE,
    
    # 原始数据tab
    tabPanel(
      "Original Zeitreihe",
      fluidPage(
        sidebarPanel(
          selectInput(
            inputId = "ori_variable", # 修改为唯一ID
            label = "Zeitreihe selektieren",
            choices = unique(data$variable),
            selected = "V1")
        ),
        mainPanel(
          plotOutput("plot", click = "plot_click"),
          verbatimTextOutput("info")
        )
      )
    ),
    
    # 预测数据tab
    tabPanel(
      "Forecast",
      fluidPage(
        sidebarPanel(
          selectInput(
            inputId = "forecast_variable", # 修改为唯一ID
            label = "Zeitreihe selektieren",
            choices = unique(data2$variable),
            selected = "V6")
        ),
        mainPanel(
          plotOutput("plot2", click = "plot_click2"),
          verbatimTextOutput("info2")
        )
      )
    )
  )


#### 服务端部分
server <- function(input, output,  session) {
  
  output$plot <- renderPlot({
    data %>%
      filter(variable == input$ori_variable) %>% # 对应修改后的ID
      ggplot(aes(dates, value, group = 1)) +
      geom_line( color="steelblue", size = 1.2) + 
      geom_point(size = 2.5) +
      xlab("") +
      ylab("Absatzmenge") +
      scale_x_date(date_breaks = "2 months")  +
      theme_bw() + 
      theme(
        panel.border = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
      )
  })
  output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })
  
  
  output$plot2 <- renderPlot({
    data2 %>%
      filter(variable == input$forecast_variable) %>% # 对应修改后的ID
      ggplot(aes(dates, value, group = 1)) +
      geom_line( color="steelblue", size = 1.2) +
      geom_point(size = 2.5) +
      xlab("") +
      ylab("Absatzmenge") +
      scale_x_date(date_breaks = "2 months")  +
      theme_bw() +
      theme(
        panel.border = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
      )
  })
  # 移到renderPlot外部单独定义
  output$info2 <- renderText({
    paste0("x=", input$plot_click2$x, "\ny=", input$plot_click2$y)
  })
  
}

runApp(list(ui = ui, server = server),host="127.0.0.1",port=9999, launch.browser = TRUE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 01:15:04