1
我正在嘗試構建一個應用程序,用戶在單擊特定對象時能夠切換選項卡。但是,我已經開發了使用模塊的應用程序,並希望繼續這樣做。在嘗試從模塊內部調用updateNavbarPage()
函數時,我遇到了範圍問題。我已經創建了一個MWE示例來說明問題。從閃亮模塊中的updateNavbarPage()函數的範圍問題
#==================================================
# MRE for updateNavBar scoping issue within modules
#==================================================
modOneUI <- function(id){
ns <- NS(id)
tagList(
h4(
"Click this button to change tabs!"
),
actionButton(
ns("submit"),
label = "Go to next Tab"
)
)
}
modOne <- function(input, output, session){
observeEvent(input$submit, {
updateNavbarPage(session, "nav-page", "tab2")
})
}
ui <- shinyUI(
navbarPage(
id = "nav-page",
title = "Example Navbar Page Issue",
tabPanel(
id = "tab1",
value = "tab1",
div(
"Tab 1"
),
div(
modOneUI("tab1_mod")
)
),
tabPanel(
id = "tab2",
value = "tab2",
div(
"Tab 2"
),
div(
h4("This is the second tab")
)
)
)
)
server <- shinyServer(function(input, output, session){
callModule(modOne, "tab1_mod")
})
shinyApp(ui = ui, server = server)
當這個應用程序運行,並在第一個選項卡上單擊操作按鈕時,什麼都不會發生。但是,如果您刪除模塊並將ui和服務器模塊代碼直接放入UI和服務器部分,則單擊此按鈕即可使用。這裏是刪除模塊的代碼。
ui <- shinyUI(
navbarPage(
id = "nav-page",
title = "Example Navbar Page Issue",
tabPanel(
id = "tab1",
value = "tab1",
div(
"Tab 1"
),
div(
h4(
"Click this button to change tabs!"
),
actionButton(
"submit",
label = "Go to next Tab"
)
)
),
tabPanel(
id = "tab2",
value = "tab2",
div(
"Tab 2"
),
div(
h4("This is the second tab")
)
)
)
)
server <- shinyServer(function(input, output, session){
observeEvent(input$submit, {
updateNavbarPage(session, "nav-page", "tab2")
})
})
shinyApp(ui = ui, server = server)
有沒有辦法使用來自updateNavbarPage()
一個模塊內切換到是不是模塊中的標籤?
謝謝!這似乎是奇怪的行爲,但它肯定會爲我解決我的問題! – tbradley