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

如何在Shiny中使用ggplot2绘制数据框子集并解决运行报错

问题修复指南

核心问题说明

你遇到的两个报错、图表不显示的问题都是代码中的语法错误、逻辑错误导致的,具体问题如下:

  • cat报错原因:output$location使用renderText组件,要求返回字符串类型的内容,但你在函数内部返回了subset筛选得到的数据框(列表类型),renderText无法处理列表类输出,所以触发报错。
  • 图表不显示原因:所有绘图代码中都写了错误的筛选逻辑covid[covid$total_cases %in% "A", ],total_cases是数值型的累计确诊字段,不可能等于字符"A",所以筛选后得到的是空数据集,自然无法生成图表;同时你所有绘图都没有用到下拉框选中的国家input$location做筛选,完全没有实现按国家切换数据的需求。
  • selectInput警告与语法错误:第一你把selected = NULL参数写在了selectInput的括号外面,属于语法错误,会导致组件配置异常;第二choices = covid$location会把数据里重复出现的国家名全部列为选项,去重后只有45个选项,不存在选项过多的问题,修改为choices = unique(covid$location)即可消除警告。

修复后参考代码

library(shiny)
library(tidyverse)
covid <- read.csv("D:/R/Practice 3/EuropeIndia.csv")

# 定义UI
ui <- fluidPage(
  titlePanel("死亡数据与各指标关联散点图"),
  sidebarLayout(
    sidebarPanel(
      selectInput("location", "选择国家",
                  choices = unique(covid$location), 
                  selected = unique(covid$location)[1]) # 默认选中第一个国家
    ),
    mainPanel(
      textOutput("selected_location"), # 改个输出id避免和input重名
      tabsetPanel(
        type = "tabs",
        tabPanel("确诊数",plotOutput("plot1t"), plotOutput("plot1n")),
        tabPanel("疫苗接种总量",plotOutput("plot2t"), plotOutput("plot2n")),
        tabPanel("接种人数",plotOutput("plot3t"),plotOutput("plot3n")),
        tabPanel("全程接种人数",plotOutput("plot4t"), plotOutput("plot4n")),
        tabPanel("加强针接种量",plotOutput("plot5t"), plotOutput("plot5n")),
        tabPanel("新增接种量",plotOutput("plot6t"),plotOutput("plot6n")),
        tabPanel("年龄中位数",plotOutput("plot7t"),plotOutput("plot7n")),
        tabPanel("新增确诊",plotOutput("plot8t"), plotOutput("plot8n")),
        tabPanel("数据汇总", verbatimTextOutput("summary")),
        tabPanel("完整数据集", tableOutput("dataset"))
      )
    )
  )
)

server <- function(input, output) {
  # 响应式筛选选中国家的数据,复用减少重复代码
  filtered_data <- reactive({
    subset(covid, location == input$location)
  })
  
  # 展示选中的国家名
  output$selected_location <- renderText({
    paste0("当前选中国家:", input$location)
  })
  
  # 所有绘图复用筛选后的数据
  output$plot1t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=total_cases)) +
      geom_point()
  })
  output$plot1n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=total_cases)) +
      geom_point()
  })
  output$plot2t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=total_vaccinations)) +
      geom_point()
  })
  output$plot2n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=total_vaccinations)) +
      geom_point()
  })
  output$plot3t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=people_vaccinated)) +
      geom_point()
  })
  output$plot3n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=people_vaccinated)) +
      geom_point()
  })
  output$plot4t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=people_fully_vaccinated)) +
      geom_point()
  })
  output$plot4n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=people_fully_vaccinated)) +
      geom_point()
  })
  output$plot5t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=total_boosters)) +
      geom_point()
  })
  output$plot5n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=total_boosters)) +
      geom_point()
  })
  output$plot6t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=new_vaccinations)) +
      geom_point()
  })
  output$plot6n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=new_vaccinations)) +
      geom_point()
  })
  output$plot7t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=median_age)) +
      geom_point()
  })
  output$plot7n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=median_age)) +
      geom_point()
  })
  output$plot8t <- renderPlot({
    ggplot(filtered_data(), aes(y=total_deaths, x=new_cases)) +
      geom_point()
  })
  output$plot8n <- renderPlot({
    ggplot(filtered_data(), aes(y=new_deaths, x=new_cases)) +
      geom_point()
  })
  output$summary <- renderPrint({
    summary(filtered_data())
  })
  output$dataset <- renderTable({
    filtered_data()
  })
}

# 运行应用
shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者Hari Chandana Bollimpalli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 22:24:04