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

如何在Shiny应用中用同一组依赖过滤器对接两个数据集生成多输出

解决方案代码
library(readxl)
library(shiny)
library(tidyverse)

# 加载两个目标数据集
TaxData <- Sample_Tax_Data
ProdData <- Sample_Prod_Data

shinyApp(
  ui = pageWithSidebar(
    headerPanel("Find My Royalty Interest"),
    sidebarPanel(
      selectizeInput(inputId = "County",
                     label = "Select County",
                     choices = NULL,
                     options = list(placeholder = 'select')
      ),
      
      selectizeInput(inputId = "Operator",
                     label = "Select Operator",
                     choices = NULL,
                     options = list(placeholder = 'select',
                                    onInitialize = I('function() { this.setValue(""); }')
                     )),
      
      selectizeInput(inputId = "Lease",
                     label = "Select Lease",
                     choices = NULL,
                     options = list(placeholder = 'select',
                                    onInitialize = I('function() { this.setValue(""); }')
                     )),
      
      selectizeInput(inputId = "Owner",
                     label = "Select Owner",
                     choices = NULL,
                     options = list(placeholder = 'select',
                                    onInitialize = I('function() { this.setValue(""); }')
                     )),
      
    ),
    
    mainPanel(
      # 用标签页区分两个输出,优化页面布局
      tabsetPanel(
        tabPanel("税务扣除信息", tableOutput("TaxTable")),
        tabPanel("产量估值信息", tableOutput("ValuationTable"))
      )
    )
  ),
  
  server = function(input, output, session) {
    
    # 筛选税务数据的响应式逻辑
    TaxFil <- reactive({
      TaxData %>%
        filter(County == input$County) %>%
        filter(Operator == input$Operator) %>%
        filter(Lease == input$Lease) %>%
        filter(Owner == input$Owner) 
    })
    
    # 复用相同筛选参数处理产量估值数据
    ProdFil <- reactive({
      ProdData %>%
        filter(County == input$County) %>%
        filter(Operator == input$Operator) %>%
        filter(Lease == input$Lease) %>%
        filter(Owner == input$Owner) 
    })
    
    # 初始化County选项:合并两个数据集的唯一值,确保覆盖所有可能的筛选项
    updateSelectizeInput(
      session = session,
      inputId = "County",
      choices = sort(unique(c(as.character(TaxData$County), as.character(ProdData$County)))),
      selected = "",
      options = list(placeholder = 'select'),
      server = TRUE
    )
    
    # 根据选中County更新Operator选项:合并两个数据集对应County下的Operator
    observeEvent(input$County,{
      choice_Operator <- sort(unique(
        c(as.character(TaxData$Operator[TaxData$County==input$County]),
          as.character(ProdData$Operator[ProdData$County==input$County]))
      ))
      
      updateSelectizeInput(
        session = session,
        inputId = "Operator",
        choices = choice_Operator,
        selected = ""
      )
    })
    
    # 根据选中Operator更新Lease选项:合并两个数据集对应County+Operator下的Lease
    observeEvent(input$Operator,{
      choice_Lease <- sort(unique(
        c(as.character(TaxData$Lease[TaxData$County==input$County & TaxData$Operator==input$Operator]),
          as.character(ProdData$Lease[ProdData$County==input$County & ProdData$Operator==input$Operator]))
      ))
      
      updateSelectizeInput(
        session = session,
        inputId = "Lease",
        choices = choice_Lease,
        selected = ""
      )
    })
    
    # 根据选中Lease更新Owner选项:合并两个数据集对应County+Operator+Lease下的Owner
    observeEvent(input$Lease,{
      choice_Owner <- sort(unique(
        c(as.character(TaxData$Owner[TaxData$County==input$County & TaxData$Operator==input$Operator & TaxData$Lease==input$Lease]),
          as.character(ProdData$Owner[ProdData$County==input$County & ProdData$Operator==input$Operator & ProdData$Lease==input$Lease]))
      ))
      
      updateSelectizeInput(
        session = session,
        inputId = "Owner",
        choices = choice_Owner,
        selected = ""
      )
    })
    
    # 渲染税务扣除表格
    output$TaxTable <- renderTable({TaxFil()})
    # 渲染产量估值表格
    output$ValuationTable <- renderTable({ProdFil()})
    
  },
  
  options = list(height = 500)
  
)
关键改动说明
  • 双数据源加载:新增加载Sample_Prod_Data,确保两个数据集都能被系统调用
  • UI布局优化:使用tabsetPanel将两个输出分标签展示,避免页面内容拥挤,提升用户操作体验
  • 过滤器选项合并:所有下拉筛选的选项均合并两个数据集的对应字段值,确保用户能筛选到任意一个数据集内的记录,不会出现选项缺失的情况
  • 筛选逻辑复用:新增ProdFil响应式表达式,完全复用现有input筛选参数,实现同一组过滤器同时对接两个数据集
  • 选中值重置:在更新下级下拉选项时添加selected = "",确保每次切换上级选项时,下级选项回到初始状态,避免无效筛选结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 03:30:01