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

R语言Reactive Dashboard:ggplot无数据及复选框异常问题求助

问题修复方案

问题1:复选框控件选项异常

错误原因

  1. 直接使用places变量引用列值,但该变量未在UI环境中定义,R会误将其识别为全局环境对象,最终返回所有列名而非目标列的唯一值
  2. 语法错误:checkboxGroupInput的selected参数被错误放置在函数括号外,导致参数传递失效

修复步骤

  • 将choices = unique(places)替换为choices = unique(my_places$places),明确引用转换后数据框的places列
  • 将selected = c("Clubs")移至checkboxGroupInput的函数括号内部,修正语法结构

问题2:ggplot无数据展示

错误原因

  1. 过滤条件错误:误用文本型列Rating与数值评分范围比较,应使用pivot后的数值型rating列
  2. 未处理复选框空选情况:此时places %in% input$desired_places会过滤掉所有数据
  3. 未应用first_timers复选框的筛选逻辑
  4. 直接绘制原始未聚合数据:每行对应单个用户的单一场所评分,直接用geom_bar(stat="identity")会导致大量数据重叠,无法正常展示

修复步骤

  • 修正过滤条件:将Rating <= input$numeric_rating[2]改为rating <= input$numeric_rating[2]
  • 添加复选框空值处理:未选择任何场所时默认展示所有场所
  • 加入First.Stay筛选逻辑:根据input$first_timers的值过滤数据
  • 聚合数据:按places和Overall分组计算平均评分后再绘图

完整修正代码

rm(list=ls())
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(tidyr)

# 读取并转换数据
my_data <- read.csv("tripadvisor_reviews.csv", header = TRUE, sep = ";")
my_places <- my_data %>%
    pivot_longer(cols = 3:12, names_to= "places", values_to = "rating")

# 定义UI
ui <- fluidPage(theme = shinytheme("cyborg"), 
                h1("XX"), 
                h2("Made by vopuidi"), 
                p("xx"), 
                br(), 
                
                sidebarLayout(
                    sidebarPanel(   
                        # 输入1:数值评分范围
                        sliderInput("numeric_rating", "Select the desired numeric rating", 
                                    min = 0, max = 5, value = c(0,5), step = 0.5),
                        
                        # 输入2:场所选择复选框
                        checkboxGroupInput("desired_places", "Select all desired amusements", 
                                           choices = unique(my_places$places),
                                           selected = c("Clubs")),
                        
                        # 输入3:首次入住筛选
                        checkboxInput("first_timers", "Display reviews by people with first stay", FALSE)
                    ),
                    mainPanel(
                        plotOutput("myscatter")
                    )
                )
)

# 定义Server逻辑
server <- function(input, output) {
    output$myscatter = renderPlot({
        # 处理复选框未选择的情况
        selected_places <- if(length(input$desired_places) == 0) {
            unique(my_places$places)
        } else {
            input$desired_places
        }
        
        # 数据过滤与聚合
        plot_data <- my_places %>%
            # 评分范围过滤
            filter(rating >= input$numeric_rating[1] & rating <= input$numeric_rating[2]) %>%
            # 场所过滤
            filter(places %in% selected_places) %>%
            # 首次入住过滤
            {if(input$first_timers) filter(., First.Stay == TRUE) else .} %>%
            # 按场所和Overall分组计算平均评分
            group_by(places, Overall) %>%
            summarise(avg_rating = mean(rating, na.rm = TRUE), .groups = "drop")
        
        # 绘制柱状图
        ggplot(plot_data, aes(x= places, y = avg_rating , fill = factor(Overall)))+
            geom_bar(position="dodge", stat="identity")+
            labs(x = "场所类型", y = "平均评分", fill = "总体评分")+
            theme(axis.text.x = element_text(angle = 45, hjust = 1))
    })
}

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 19:33:12