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

R Shiny模块化下如何用spsComps::addLoader实现下拉输入加载Spinner

解决Shiny模块化开发中spsComps::addLoader加载Spinner失效问题

你的问题核心是模块命名空间的ID未正确处理:模块化架构下,所有UI元素的ID会自动带上模块命名空间前缀(比如你的模块ID是dropdowns,原始IDsubspecies_container会被渲染成dropdowns-subspecies_container),但你创建loader时仍使用未加命名空间的原始ID,导致addLoader找不到目标容器,Spinner无法正常显示。

修复步骤

  1. UI模块中给容器ID添加命名空间:在tab1_ui.R里,用ns()包裹容器ID,确保渲染后的ID带正确前缀。
  2. 服务器模块中使用带命名空间的选择器:创建loader时,通过session$ns()获取带命名空间的目标元素ID,而非直接使用原始ID。

修改后的完整代码

global.R

library(shiny)
library(dplyr)
library(spsComps) # 补充加载spsComps包

source("tab1_ui.R", local = T)
source("tab1_server.R", local = T)

# 构造包含亚种列的数据集
iris2 <- iris %>% 
  mutate(
    subspecies = case_when(
      startsWith(as.character(Species), "setosa") ~ rep(c("setosa1", "setosa2"), length.out = n()),
      startsWith(as.character(Species), "versicolor") ~ rep(c("versicolor1", "versicolor2"), length.out = n()),
      startsWith(as.character(Species), "virginica") ~ rep(c("virginica1", "virginica2"), length.out = n())
    )
) 

tab1_ui.R

dropdownsUI <- function(id) {
  ns <- NS(id) 
  
  tagList(
    selectInput(ns("speciesDropdown"), label = "物种:", choices = c("setosa", "versicolor", "virginica")),
    
    div(
      id = ns("subspecies_container"), # 用ns()包裹容器ID
      uiOutput(ns("subspeciesDropdown"))
    )
  )
}

tab1_server.R

subspeciesServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    dependent_subspecies <- reactive({
      iris2 %>%
        filter(Species == req(input$speciesDropdown)) %>% 
        pull(subspecies) %>%
        unique()
    })
    
    # 通过session$ns获取带命名空间的容器选择器
    subspecies_loader <- spsComps::addLoader$new(target_selector = session$ns("subspecies_container"))
    
    output$subspeciesDropdown <- renderUI({
      subspecies_loader$show()
      on.exit({subspecies_loader$hide()})
      
      Sys.sleep(1) # 模拟延迟加载效果
      
      # 下拉框ID同样需要ns()包裹,避免后续取值异常
      selectInput(ns("vars_subspecies"), "亚种:", choices = dependent_subspecies())
    })
      
    }
  )
}

额外说明

原代码中selectInput("vars_subspecies")未添加ns(),这会导致后续获取该输入值时出现找不到元素的问题,因此一并做了修正。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 01:45:42