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

借助Shiny为Flexdashboard图表添加筛选功能

Hey there! Let's get your Flexdashboard + Shiny chart set up with filtering functionality. First, I'll wrap up your sample data frame (since it looks cut off) and then walk through a complete, working example.

Step 1: Clean up your data

First, let's finish defining your data frame properly and make sure the date column is in a usable format:

# 生成日期序列
date <- seq(as.Date("2017/1/1"), as.Date("2018/1/1"), by = "month")
date <- as.character(date[-1]) # 保留12个月份数据

# 构建数据框并命名列
proto <- as.data.frame(matrix(
  c(1,1,1,2,2,3,3,3,3,4,4,4, 
    0,0,0,0,1,1,1,1,1,2,2,2, 
    5,5,5,6,6,7,7,7,7,7,8,8), 
  ncol = 3, nrow = length(date), byrow = FALSE
))
names(proto) <- c("cap_1", "cap_2", "cap_3")
proto$date <- as.Date(date) # 转换为Date类型,方便后续日期筛选

Step 2: Full Flexdashboard with Filters

Here's a complete dashboard example that includes dropdown filters for cap_1 and cap_2, plus a dynamic chart that updates based on your selections:

---
title: "带筛选功能的仪表盘"
runtime: shiny
output: flexdashboard::flex_dashboard
---

```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
# 加载整理好的数据
date <- seq(as.Date("2017/1/1"), as.Date("2018/1/1"), by = "month")
date <- as.character(date[-1])
proto <- as.data.frame(matrix(
  c(1,1,1,2,2,3,3,3,3,4,4,4, 
    0,0,0,0,1,1,1,1,1,2,2,2, 
    5,5,5,6,6,7,7,7,7,7,8,8), 
  ncol = 3, nrow = length(date), byrow = FALSE
))
names(proto) <- c("cap_1", "cap_2", "cap_3")
proto$date <- as.Date(date)

筛选面板

选择筛选条件

  • 按cap_1筛选:
    selectInput("cap1_filter", label = "选择cap_1的值:",
                choices = unique(proto$cap_1),
                selected = unique(proto$cap_1)[1])
    
  • 按cap_2筛选:
    selectInput("cap2_filter", label = "选择cap_2的值:",
                choices = unique(proto$cap_2),
                selected = unique(proto$cap_2)[1])
    

动态图表

cap_3趋势图

renderPlot({
  # 根据筛选条件过滤数据
  filtered_data <- subset(proto, 
                          cap_1 == input$cap1_filter & 
                          cap_2 == input$cap2_filter)
  
  # 绘制折线图(可根据需求替换为柱状图、散点图等)
  ggplot(filtered_data, aes(x = date, y = cap_3)) +
    geom_line(color = "#2c3e50", size = 1.2) +
    geom_point(color = "#e74c3c", size = 3) +
    labs(title = "cap_3随日期变化趋势",
         x = "日期", y = "cap_3数值") +
    theme_minimal() +
    theme(plot.title = element_text(hjust = 0.5, size = 16))
})
### Key Notes:
- Don't forget to include `runtime: shiny` in your YAML header—this is what enables Shiny's interactive functionality within Flexdashboard.
- The `renderPlot` function automatically refreshes the chart whenever you adjust the filter selections, as it pulls real-time values from `input$cap1_filter` and `input$cap2_filter`.
- If you prefer a date range filter instead of category dropdowns, you can swap out one of the inputs with this:
  ```r
  dateRangeInput("date_range", label = "选择日期范围:",
                 start = min(proto$date),
                 end = max(proto$date),
                 min = min(proto$date),
                 max = max(proto$date))

Then update the filtering logic to:

filtered_data <- subset(proto, 
                        date >= input$date_range[1] & 
                        date <= input$date_range[2])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:31:33