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

如何在Flexdashboard中使用Shiny实现表格的赛季筛选功能?

问题修正方案

原代码存在的核心问题

  • 下拉选择器的选项包含重复值:choices = data$Season会生成重复的赛季条目
  • 表格筛选逻辑错误:data[,input$season]是按列索引提取数据,而非筛选对应赛季的行数据
  • 输入控件与表格布局不合理:表格放在侧边栏会导致显示空间不足

方案1:使用模块的修正版(适合模块化开发)

---
output:
  flexdashboard::flex_dashboard:
    orientation: columns
runtime: shiny
---

```{r libraries, include=FALSE}
library(tidyverse)
library(flexdashboard)
Seasons

Inputs {.sidebar data-width=250}

seasonUI <- function(id){
  ns <- NS(id)
  inputPanel(
    # 生成去重且排序的赛季选项
    selectInput(ns("season"), "选择赛季", choices = sort(unique(data$Season)))
  )
}

seasonSERVER <- function(input, output, session) {
  # 创建响应式筛选数据
  filtered_data <- reactive({
    filter(data, Season == input$season)
  })
  
  return(filtered_data)
}

# 调用模块并获取响应式数据
filtered_data <- callModule(seasonSERVER, "seasons")
seasonUI("seasons")

赛季数据

筛选后的玩家数据

renderTable({
  filtered_data()
})
---

### 方案2:无模块简化版(适合新手快速上手)
```yaml
---
output:
  flexdashboard::flex_dashboard:
    orientation: columns
runtime: shiny
---

```{r libraries, include=FALSE}
library(tidyverse)
library(flexdashboard)
Seasons

Inputs {.sidebar data-width=250}

# 直接定义下拉选择器,选项去重排序
selectInput("season", "选择赛季", choices = sort(unique(data$Season)))

赛季数据

筛选后的玩家数据

# 响应输入变化,筛选对应赛季的行
renderTable({
  filter(data, Season == input$season)
})
---

### 关键修正点说明
1. **选择器优化**:用`sort(unique(data$Season))`生成唯一且有序的赛季选项,避免重复值
2. **筛选逻辑修正**:使用`dplyr::filter()`按Season列匹配选中值,提取对应行的完整数据
3. **布局调整**:将表格移至主面板,保证数据显示空间充足
4. **响应式数据处理**:通过`reactive()`或直接在`renderTable()`中响应输入变化,实现实时筛选

内容的提问来源于stack exchange,提问作者Logan W.
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 09:06:38