Reputation: 519
I want to build my shinydashboard page programmatically and I also need to programmatically change the page shown, e.g. after successful login. I have closely followed the example in ?updateTabItems
but somehow it appears as if updateTabItems
does not work when the content is generated programmatically. Here is my code which does not show page2 after pressing the login button.
# create ui that is created dynamically from server logic
ui <- dashboardPage(
dashboardHeader(title = "switch tabs"),
dashboardSidebar(sidebarMenu(sidebarMenuOutput("sidebarMenu"))),
dashboardBody(uiOutput("body"))
)
# build ui of login page
uiLogin = function(id) {
ns = NS(id)
actionButton( ns("loginButton"), "Login")
}
# server logic of login page
module = function(input, outpust, session) {
# observe login button
observeEvent(input$loginButton, {
updateTabItems(session, "sidebarID", selected = "loginTab")
cat("trying to switch tab\n")
})
}
# server logic
server <- function(input, output, session) {
# sidebar menu
output$sidebarMenu <- renderMenu({
sidebarMenu(
id="sidebarID",
menuItem("Login", tabName = "loginTab", selected=TRUE),
menuItem("Page 2", tabName = "page2")
)
})
# body
output$body <- renderUI(
tabItems(
tabItem(tabName="loginTab", uiLogin("loginPage")),
tabItem(tabName="page2", tags$h2("Page 2"))
)
)
# server logic of login page
callModule(module, "loginPage")
}
shinyApp(ui, server)
Interestingly, not even the login page is shown upon start, although selected=TRUE
was set in tabItem
. My shiny version is 1.2.0 and shinydashboard is on version 0.7.1.
Any suggestions how I can update the view programmatically are much appreciated?
Upvotes: 0
Views: 895
Reputation: 519
As so often in (my) shiny applications, the problem was in the namespace. Passing the parent session to the module of the login page
# server logic of login page
callModule(module, "loginPage", session)
and updating the tab item in the proper namespace allows to change the tab
# server logic of login page
module = function(input, output, session, parentSession) {
# observe login button
observeEvent(input$loginButton, {
updateTabItems(parentSession, "sidebarID", selected = "loginTab")
})
}
I did not realize that in the example given in ?updateTabItems
the sidebar ID and the module reside in the same namespace, while the namespace was different in my example.
Upvotes: 3