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

基于R Shiny与dplyr实现带多维度筛选的评分可视化仪表盘开发问询

嘿,我来帮你补全这个Shiny交互式仪表盘的代码,完全匹配你提到的多维度筛选、均值计算和可视化需求,直接就能运行:

library(shiny)
library(ggplot2)
library(dplyr)

# 先模拟一份示例数据集(你可以替换成自己的真实数据)
set.seed(123)
sample_data <- tibble(
  year = sample(2020:2023, 500, replace = TRUE),
  status = sample(c("Active", "Inactive", "New"), 500, replace = TRUE),
  product = sample(c("Product A", "Product B", "Product C", "Product D"), 500, replace = TRUE),
  satisfaction_score = runif(500, 1, 10),
  importance_score = runif(500, 1, 10)
)

ui <- fluidPage(
  titlePanel("产品满意度与重要性分析仪表盘"),
  
  sidebarLayout(
    sidebarPanel(
      # 年份筛选器(支持多选)
      selectInput(
        inputId = "year_filter",
        label = "选择年份:",
        choices = unique(sample_data$year),
        selected = unique(sample_data$year),
        multiple = TRUE
      ),
      # 客户状态筛选器
      selectInput(
        inputId = "status_filter",
        label = "选择客户状态:",
        choices = unique(sample_data$status),
        selected = unique(sample_data$status),
        multiple = TRUE
      ),
      # 产品筛选器
      selectInput(
        inputId = "product_filter",
        label = "选择产品:",
        choices = unique(sample_data$product),
        selected = unique(sample_data$product),
        multiple = TRUE
      )
    ),
    
    mainPanel(
      plotOutput("score_plot"),
      br(),
      tableOutput("summary_table")
    )
  )
)

server <- function(input, output) {
  # 响应式筛选数据集
  filtered_data <- reactive({
    # 确保筛选条件不为空才执行
    req(input$year_filter, input$status_filter, input$product_filter)
    
    sample_data %>%
      filter(
        year %in% input$year_filter,
        status %in% input$status_filter,
        product %in% input$product_filter
      )
  })
  
  # 计算各产品的得分均值,整理成data.frame
  score_summary <- reactive({
    req(filtered_data())
    
    filtered_data() %>%
      group_by(product) %>%
      summarise(
        avg_satisfaction = mean(satisfaction_score, na.rm = TRUE),
        avg_importance = mean(importance_score, na.rm = TRUE),
        sample_size = n(), # 额外加了样本量,方便判断结果可信度
        .groups = "drop"
      ) %>%
      # 过滤掉有缺失值的产品
      filter(!is.na(avg_satisfaction) & !is.na(avg_importance))
  })
  
  # 可视化得分对比图
  output$score_plot <- renderPlot({
    req(score_summary())
    
    ggplot(score_summary(), aes(x = avg_importance, y = avg_satisfaction)) +
      # 散点图:大小对应样本量,颜色区分产品
      geom_point(aes(size = sample_size, color = product), alpha = 0.7) +
      # 添加对角线参考线(满意度=重要性),快速识别待优化产品
      geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "gray50") +
      labs(
        x = "平均重要性得分",
        y = "平均满意度得分",
        title = "产品重要性与满意度对比",
        size = "样本量",
        color = "产品"
      ) +
      theme_minimal() +
      scale_size(range = c(3, 8)) # 控制点的大小范围
  })
  
  # 展示均值汇总表格
  output$summary_table <- renderTable({
    score_summary() %>%
      select(product, avg_satisfaction, avg_importance, sample_size) %>%
      rename(
        产品 = product,
        平均满意度 = avg_satisfaction,
        平均重要性 = avg_importance,
        样本量 = sample_size
      ) %>%
      mutate(
        平均满意度 = round(平均满意度, 2),
        平均重要性 = round(平均重要性, 2)
      )
  })
}

shinyApp(ui = ui, server = server)

关键功能说明

  • 多维度筛选:三个筛选器都支持多选,用户可以自由组合年份、状态、产品条件,筛选结果实时更新。
  • 均值计算:通过dplyr按产品分组计算两个得分的均值,同时保留了样本量(毕竟小样本的均值参考性弱),最终输出的就是你需要的data.frame格式。
  • 可视化:用散点图把两个得分放在同一张图里,对角线能帮你快速定位那些“用户觉得重要但满意度低”的产品(落在线下方的点),这些就是优先优化的对象。
  • 容错处理:用req()确保只有当筛选后有有效数据时才生成图表和表格,避免空数据导致的报错。

自定义小技巧

  • 替换数据集:把sample_data换成你的真实数据就行,只要列名(year/status/product/satisfaction_score/importance_score)对应上。
  • 调整图表:如果想加象限划分,直接在ggplot里加geom_vline(xintercept = 5)和geom_hline(yintercept = 5)(假设得分是1-10),把画布分成四个象限。
  • 样式修改:换颜色主题、调整字体大小,直接修改ggplot的theme参数就行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:08:36