SeG*_*eGa 4 r shiny shinymodules
我想创建一个navbarPage,其中每个tabPanel或tagList每个tabPanel都是在另一个模块中创建的。
在“正常”的闪亮应用程序中,我将能够通过使用知道当前选择了哪个选项卡input$navbarPage_ID。(其中navbarPage_ID是分配的 id 变量navbarPage)
对于模块,我无法获得正确的 ID,因为它不会改变。
如何获取服务器模块中所选选项卡的正确ID?
示例 1,tabPanels在模块中创建:
library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod1")
)
}
mod1_server <- function(input, output, session, navid) {
observe({
message("mod1_server ", navid)
})
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod2")
)
}
mod2_server <- function(input, output, session, navid) {
observe({
message("mod2_server ", navid)
})
}
## Shiny App #####################
ui <- navbarPage(collapsible = T, id = "navbarid",
title = "Title",
mod1_ui("mod1", "Module 1 Tab", navid = 1),
mod2_ui("mod2", 'Module 2 Tab', navid = 2)
)
server <- function(input, output, session) {
callModule(mod1_server, "mod1", input$navbarid)
callModule(mod2_server, "mod2", input$navbarid)
}
shinyApp(ui, server)Run Code Online (Sandbox Code Playgroud)
示例 2,其中tabPanels在 UI 中创建并且仅tagList在模块中创建:
library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
ns <- NS(id)
tagList(h2("mod1"))
}
mod1_server <- function(input, output, session, navid) {
observe({
message("mod1_server ", navid)
# message("mod1_server ", input$navbarid)
})
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
ns <- NS(id)
tagList(h2("mod2"))
}
mod2_server <- function(input, output, session, navid) {
observe({
message("mod2_server ", navid)
# message("mod2_server ", input$navbarid)
})
}
## Shiny App #####################
ui <- navbarPage(collapsible = T, id = "navbarid",
title = "Title",
tabPanel("Module 1 Tab", value = 1,
mod1_ui("mod1")
),
tabPanel("Module 2 Tab", value = 2,
mod2_ui("mod2")
)
)
server <- function(input, output, session) {
callModule(mod1_server, "mod1", input$navbarid)
callModule(mod2_server, "mod2", input$navbarid)
}
shinyApp(ui, server)Run Code Online (Sandbox Code Playgroud)
您mod1_server在服务器中调用了两次;p
我将在父会话中使用这个技巧:
library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod1")
)
}
mod1_server <- function(input, output, session, parent_session) {
observe({
message("mod1_server ", parent_session$input$navbarid)
})
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod2")
)
}
mod2_server <- function(input, output, session, parent_session) {
observe({
message("mod2_server ", parent_session$input$navbarid)
})
}
## Shiny App #####################
ui <- navbarPage(collapsible = T, id = "navbarid",
title = "Title",
mod1_ui("mod1", "Module 1 Tab", navid = 1),
mod2_ui("mod2", 'Module 2 Tab', navid = 2)
)
server <- function(input, output, session) {
callModule(mod1_server, "mod1", parent_session = session)
callModule(mod2_server, "mod2", parent_session = session)
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)