一个闪亮模块中的多个 tabItems [英] Multiple tabItems in one shiny module
问题描述
你好,我对 R 编程和 Shiny 比较陌生.我目前正在开发一个闪亮的仪表板应用程序.我一直在解决如何在一个模块中包含多个 tabItem 的问题.在实际的应用程序中,我需要在模块之间传递更多信息,而子菜单并不相同.那么有没有办法让这个工作?非常感谢您的帮助!
Hello im relatively new to R Programming and Shiny. I´m currently developing an shiny dashboard application. I´m stuck a the moment with the problem of how to have multiple tabItems in one module. In the real app I need to pass a lot more information's between the modules and the submenu's aren't alike. So is there a way to make this work? Thanks so much for your help!
library(shiny)
library(shinydashboard)
library(shinydasboardPlus)
#submodules
submodule_ui <- function(id,tabName){
ns <- NS(id)
tabItem(
tabName = tabName,
boxPlus(
title = "some title",
textOutput(ns("some_output"))
)
)
}
submodule_server <- function(id,see){
moduleServer(
id,
function(input, output, session){
output$some_output <- renderText({
see
})
}
)
}
#module
module_ui <- function(id,tabName1,tabName2){
ns <- NS(id)
submodule_ui(ns("sub1"),
tabName = tabName1)
submodule_ui(ns("sub2"),
tabName = tabName2)
}
module_server <- function(id){
moduleServer(
id,
function(input, output, session){
submodule_server("sub1","hello")
submodule_server("sub2","world !")
}
)
}
#app
ui <- dashboardPagePlus(
header = dashboardHeaderPlus(
title = "dummy app"
),
sidebar = dashboardSidebar(
sidebarMenu(
menuItem(
text = "home",
tabName = "home"
),
menuItem(
text = "submodule1",
tabName = "subtab1"
),
menuItem(
text = "submodule2",
tabName = "subtab2"
),
menuItem(
text = "some other tabItems",
tabName = "some_other_tabItems"
)
)
),
body = dashboardBody(
tabItems(
tabItem(
tabName = "home",
box(
title = "home of the app",
width = "auto"
)
),
module_ui(
id = "module",
tabName1 = "subtab1",
tabName2 = "subtab2"
),
tabItem(
tabName = "some_other_tabItems",
box(
title = "some other content"
)
)
)
)
)
server <- function(input, output){
module_server("module")
}
shinyApp(ui,server)
´´´
推荐答案
在子模块周围获取选项卡项包装器似乎存在一些问题 - 它只生成第二个子模块.模块就像函数一样,因为它们倾向于产生最终调用.您可以将内容包装在 list
或 taglist
中以返回更多项目.与此同时...
It appears there was some issues with getting the tab item wrapper around the submodules - it was only producing the second submodule. Modules act like functions as they tend to produce the final call. You can wrap things in a list
or taglist
to return more items. In the meantime...
通过将 tabItems
包装器移动到模块中,它能够正确创建列表并生成两个选项卡.
By moving the tabItems
wrapper into the module, it was able to create the list properly and produce both tabs.
注意:我将函数转换为 shinydashboard
,因为我可以找出 xxxPlus
函数的来源.
Note: I converted the functions to shinydashboard
as I could figure out where the xxxPlus
functions came from.
library(shiny)
library(shinydashboard)
#submodules
submodule_ui <- function(id,tabName){
ns <- NS(id)
tabItem(
tabName = tabName,
box(
title = "some title",
textOutput(ns("some_output"))
)
)
}
submodule_server <- function(id,see){
moduleServer(
id,
function(input, output, session){
output$some_output <- renderText({
see
})
}
)
}
#module
module_ui <- function(id,tabName1,tabName2){
ns <- NS(id)
### tabsItems now produced in module, submodules separated by comma
tabItems(
submodule_ui(ns("sub1"),
tabName = tabName1),
submodule_ui(ns("sub2"),
tabName = tabName2)
)
}
module_server <- function(id){
moduleServer(
id,
function(input, output, session){
submodule_server("sub1","hello")
submodule_server("sub2","world !")
}
)
}
#app
ui <- dashboardPage(
header = dashboardHeader(
title = "dummy app"
),
sidebar = dashboardSidebar(
sidebarMenu(
menuItem(
text = "submodule1",
tabName = "subtab1"
),
menuItem(
text = "submodule2",
tabName = "subtab2"
)
)
),
body = dashboardBody(
module_ui(
id = "module",
tabName1 = "subtab1",
tabName2 = "subtab2"
)
)
)
server <- function(input, output){
module_server("module")
}
shinyApp(ui,server)
这篇关于一个闪亮模块中的多个 tabItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!