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

R Shiny中Reactive Picker Input显示自定义标签实现方案问询

解决方案

核心原理是Shiny所有选择输入组件的choices参数都支持命名向量:向量的名称会作为前端显示的文本,向量的取值会作为后台接收的实际值,和你现有Period选择器的逻辑完全一致。

修改步骤

  1. 先从codebook中提取Polygon和Label的唯一对应关系,构造命名映射向量:
# 提取唯一映射,避免重复选项
poly_map <- unique(codebook[, c("Polygon", "Label")])
# 构造命名向量:名称为显示用的Label,值为实际用的Polygon
poly_choices <- setNames(poly_map$Polygon, poly_map$Label)
  1. 替换UI中pickerInput的choices参数为上述poly_choices
  2. 同步替换updatePickerInput中的choices参数为poly_choices,保证动态更新时显示逻辑不变
  3. 原有后端过滤逻辑不需要修改,input$picker_cny返回的仍然是Polygon原始值,不会影响后续数据筛选

修改后完整代码

# 先加载数据集,提前构造映射向量
codebook <- structure(list(X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Polygon = c("polygon_a", 
"polygon_a", "polygon_a", "polygon_a", "polygon_a", "polygon_a", 
"polygon_a", "polygon_a", "polygon_b", "polygon_b", "polygon_b", 
"polygon_b", "polygon_b", "polygon_b", "polygon_b", "polygon_b", 
"polygon_c", "polygon_c", "polygon_c", "polygon_c"), Label = c("Baron Dubois", 
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Dubois", 
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Delasalle", 
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Delasalle", 
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Istog", 
"Baron Istog", "Baron Istog", "Baron Istog"), Year = c("year_1", 
"year_1", "year_1", "year_1", "year_2", "year_2", "year_2", "year_2", 
"year_1", "year_1", "year_1", "year_1", "year_2", "year_2", "year_2", 
"year_2", "year_1", "year_1", "year_1", "year_1"), Variable = c("Variable1", 
"Variable2", "Variable3", "Variable4", "Variable1", "Variable2", 
"Variable3", "Variable4", "Variable1", "Variable2", "Variable3", 
"Variable4", "Variable1", "Variable2", "Variable3", "Variable4", 
"Variable1", "Variable2", "Variable3", "Variable4"), Value = c(1L, 
245L, 23L, 2L, 0L, 34L, 1L, 245L, 1L, 23L, 2L, 0L, 0L, 34L, 0L, 
34L, 0L, 34L, 90L, 9L)), class = "data.frame", row.names = c(NA, 
-20L))

# 构造选择器映射向量
poly_map <- unique(codebook[, c("Polygon", "Label")])
poly_choices <- setNames(poly_map$Polygon, poly_map$Label)

# Creation of APP ------------------------------------------
remove(ui, server)

if (interactive()) {
  
  library(shiny)
  library(shinyWidgets)
  library(shinythemes)
  library(shinycssloaders)
  library(shinydashboard)  
  library(DT)
  
  ##########
  
  # Define UI -----------------------------------------------
  ui <- fluidPage(
    
    # Application title
    titlePanel("Colonial Concessions Within DRC"),
    
    # Parameters
    sidebarLayout(
      sidebarPanel(
        selectInput(inputId = "input_period", label = "Period",
                    choices = c("2001" = "year_1", "2002" = "year_2", "2003" = "year_3")),
        pickerInput(
          inputId = "picker_cny",
          label = "Select Polygon",
          choices = poly_choices, # 替换为构造好的命名向量
          options = list(`actions-box` = TRUE),
          multiple = TRUE),
        width = 2),
      
      # Display the reactive table
      mainPanel(
        DTOutput("t1"),
        width = 10)
    ))
  
  # Define Server ------------------------------------------
  server <- function(input, output, session) {
    
    output$t1 <- renderDT({
      code1 <- codebook[codebook$Year == input$input_period & (codebook$Polygon %in% input$picker_cny),]
      code1
    })
    
    # Reactive pickerInput ---------------------------------
    observeEvent(input$input_period, {
      
      # Generate reactive picker input
      code1 <- codebook[codebook$Year %in% input$input_period,]
      codeu <- poly_map$Polygon # 顺序和poly_choices保持一致
      code1u <- unique(code1$Polygon)
      disabled_choices <- ifelse(codeu %in% code1u, 0,1)
      updatePickerInput(session = session,
                        inputId = "picker_cny",
                        choices = poly_choices, # 同样替换为命名向量
                        choicesOpt = list(
                          disabled = disabled_choices,
                          style = ifelse(disabled_choices,
                                         yes = "color: rgba(119, 119, 119, 0.5);",
                                         no = "")
                        )
      )
    }, ignoreInit = TRUE)
    
  }
  
  # Run the application
  shinyApp(ui = ui, server = server)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 23:57:04