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

Shiny模块中双selectInput互斥动态更新功能失效问题求助

Shiny模块中双selectInput互斥动态更新功能失效问题求助

大家好,我最近在开发Shiny应用时遇到了一个棘手的问题:我做了两个selectInput输入框,它们的选项都是上传表格的列名,要求实现互斥选择——也就是在其中一个输入框选了某列后,这列会在另一个输入框里变成不可选状态,反之亦然。单独写这段逻辑的时候代码是正常工作的,但把它封装成Shiny模块之后,这个互斥更新的功能就失效了,我找不到问题出在哪里,麻烦各位帮忙排查一下!

正常运行的代码(非模块版本)

这段代码可以完美实现我想要的互斥效果:

library(shiny)

ui <- fluidPage(
  fileInput(inputId = "rawFile",
            label = "Upload Data Table:",
            multiple = FALSE,
            accept = c(".csv")
  ),
  uiOutput(outputId = "v1",
           label = "Select Variable 1"
  ),
  uiOutput(outputId = "v2",
           label = "Select Variable 2"
  )
)

server <- function(input, output, session){
  inputData <- reactive({
    inFile <- input$rawFile
    if(is.null(inFile)){return(NULL)}
    extension <- tools::file_ext(inFile$name)
    filepath <- inFile$datapath
    df <- read.csv(filepath, header = TRUE)
    return(df)
  })
  
  output$v1 <- renderUI({
    shiny::req(inputData())
    selectInput(inputId = "v1",
                label = "Select columns to remove",
                multiple = TRUE,
                choices = names(inputData())
    )
  })
  
  output$v2 <- renderUI({
    shiny::req(inputData())
    selectInput(inputId = "v2",
                label = "Select columns to remove",
                multiple = TRUE,
                choices = names(inputData())
    )
  })
  
  observe({
    if(!is.null(input$v2))
      updateSelectInput(session, "v1",
                        choices = names(inputData())[!(names(inputData()) %in% input$v2)],
                        selected = isolate(input$v1)
      )
  })
  
  observe({
    if(!is.null(input$v1))
      updateSelectInput(session, "v2",
                        choices = names(inputData())[!(names(inputData()) %in% input$v1)],
                        selected = isolate(input$v2)
      )
  })
}

shinyApp(ui = ui, server = server)

失效的模块版本代码

我把上面的逻辑封装成模块后,互斥更新的功能就完全不起作用了,代码如下:

library(shiny)

ui_1 <- function(id){
  ns <- NS(id)
  tagList(
    fluidPage(
      fileInput(inputId = ns("rawFile"),
                label = "Upload Data Table:",
                multiple = FALSE,
                accept = c(".csv")
      ),
      uiOutput(outputId = ns("v1"),
               label = "Select Variable 1"
      ),
      uiOutput(outputId = ns("v2"),
               label = "Select Variable 2"
      )
    )
  )
}

server_1 <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    inputData <- reactive({
      inFile <- input$rawFile
      if(is.null(inFile)){return(NULL)}
      extension <- tools::file_ext(inFile$name)
      filepath <- inFile$datapath
      df <- read.csv(filepath, header = TRUE)
      return(df)
    })
    
    output$v1 <- renderUI({
      shiny::req(inputData())
      selectInput(inputId = ns("v1"),
                  label = "Select columns to remove",
                  multiple = TRUE,
                  choices = names(inputData())
      )
    })
    
    output$v2 <- renderUI({
      shiny::req(inputData())
      selectInput(inputId = ns("v2"),
                  label = "Select columns to remove",
                  multiple = TRUE,
                  choices = names(inputData())
      )
    })
    
    observe({
      if(!is.null(input$v2))
        updateSelectInput(session, ns("v1"),
                          choices = names(inputData())[!(names(inputData()) %in% input$v2)],
                          selected = isolate(input$v1)
        )
    })
    
    observe({
      if(!is.null(input$v1))
        updateSelectInput(session, ns("v2"),
                          choices = names(inputData())[!(names(inputData()) %in% input$v1)],
                          selected = isolate(input$v2)
        )
    })
  })
}

备注:内容来源于stack exchange,提问作者Wang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 13:52:41