【发布时间】:2026-01-07 06:45:01
【问题描述】:
您好,我对 R 编程非常陌生。
目前我正在使用仪表板来创建一些数据并显示它。
这个项目很快就变得相当大,所以我正在尝试模块化仪表板。
这给我带来了一些问题。一个是这个Multiple tabItems in one shiny module。
另一个是我想要/需要为用户提供一个进度条,因为数据处理需要相当长的时间。
这种数据处理现在分为多个模块,如下例所示。
但是栏不会比第一个模块更进一步。
我的猜测是 id 不匹配,因此找不到以下更新。
我不知道“隔离”updateProgressBar() 的 id 并将其传递给模块。
非常感谢您的帮助!
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
#module_1
module_1_ui <- function(id){
ns <- NS(id)
tagList(
boxPlus(
title = "some title",
textOutput(ns("some_output"))
)
)
}
module_1_server <- function(id,see){
moduleServer(
id,
function(input, output, session){
ns <- session$ns
observe({
progressSweetAlert(
id = ns("progress"),
session = session,
value = 1,
total = 4,
)
Sys.sleep(1) #dummy for some functions that take some time to process
updateProgressBar(
id = ns("progress"),
session = session,
value = 2,
total = 4
)
})
output$some_output <- renderText({
see
})
}
)
}
#module_1
module_2_ui <- function(id){
ns <- NS(id)
tagList(
boxPlus(
title = "some title",
textOutput(ns("some_output"))
)
)
}
module_2_server <- function(id,see){
moduleServer(
id,
function(input, output, session){
ns <- session$ns
observe({
updateProgressBar(
session = session,
id = ns("progress"),
value = 3,
total = 4
)
Sys.sleep(4) #dummy for some functions that take some time to process
updateProgressBar(
session = session,
id = ns("progress"),
value = 4,
total = 4
)
Sys.sleep(2)
closeSweetAlert(session = session)
})
output$some_output <- renderText({
see
})
}
)
}
#app
ui <- dashboardPagePlus(
header = dashboardHeaderPlus(
title = "dummy app"
),
sidebar = dashboardSidebar(
sidebarMenu(
menuItem(
text = "home",
tabName = "home"
),
menuItem(
text = "module_1",
tabName = "tab_1"
),
menuItem(
text = "module_2",
tabName = "tab_2"
),
menuItem(
text = "some other tabItems",
tabName = "some_other_tabItems"
)
)
),
body = dashboardBody(
tabItems(
tabItem(
tabName = "home",
box(
title = "home of the app",
width = "auto"
)
),
tabItem(
tabName = "tab_1",
module_1_ui(
id = "module_1"
)
),
tabItem(
tabName = "tab_2",
module_2_ui(
id = "module_2"
)
),
tabItem(
tabName = "some_other_tabItems",
box(
title = "some other content"
)
)
)
)
)
server <- function(input, output){
module_1_server(
id = "module_1",
see = "something happens here"
)
module_2_server(
id = "module_2",
see = "something happens here as well"
)
}
shinyApp(ui,server)
【问题讨论】:
-
不确定这是否可能,因为模块 1 中的
ns("progress")创建的 id 与模块 2 中不同。您可以将模块 2 中的ns("progress")替换为"module_1-progress"(这是由 @987654327 提供的 id @ 在模块 1) 中,但问题是updateProgressBar()中的session参数。我认为您不应该在两个模块之间划分进度条。
标签: r shiny module shinydashboard