Shiny Dashboard: Reset the conditional panel state when we navigate across different tabitems

柔情痞子 提交于 2021-02-11 07:08:01

问题


I have the following code in which there are various conditional panels in the dashboard. If we navigate from one conditional panel to next in dashboard and then go to widget and finally come back to dashboard, the dashboard is in the previous state. I want the dashboard to be refreshed(reset to original condition) when I come back from another tab panel. Is is possible to do that?

library(shiny)
library(shinydashboard)
library(maps)
library(leaflet)

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Widgets", tabName = "widgets", icon = icon("th"))
  )),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
      tags$script("
                  Shiny.addCustomMessageHandler('resetInputValue', function(variableName){
                  Shiny.onInputChange(variableName, null);
                  });
                  "),

      conditionalPanel(
        condition <- "input.link_click  === undefined || input.link_click === null",
        leafletOutput("Map", width = 1000, height = 500)

      ),


      conditionalPanel(
        condition <- "(input.link_click_Site  === undefined || input.link_click_Site === null) && (input.link_click  !== undefined && input.link_click !== null)",

        leafletOutput("CountryMap", width = 1000, height = 500)

      ),
      conditionalPanel(
        condition <- "(input.link_click_Site  !== undefined && input.link_click_Site !== null)",

        h3("Plots related to site chosen"),
        textOutput(outputId = "Check"),
        actionButton("Back", "Back")
      )
     ),
     tabItem(tabName = "widgets",
             h3("This is widget page")

             )
     )
  )
)


server <- function(input, output, session){
  Country = map("world", fill = TRUE, plot = FALSE, regions="USA")
  output$Map <- renderLeaflet({
    leaflet(Country) %>% addTiles() %>%  setView(0, 0,  zoom = 2)%>%
      #leaflet(target) %>% addTiles() %>%
      addPolygons(fillOpacity = 0.6,
                  fillColor = 'blue',
                  smoothFactor = 0.5, stroke = TRUE, weight = 1, popup =  paste("<b>", "USA", "</b><br>",
                                                                                actionLink(inputId = "View", 
                                                                                           label = "View Details", 
                                                                                           onclick = 'Shiny.onInputChange(\"link_click\",  Math.random())')))
  })

  output$CountryMap <- renderLeaflet({

    leaflet(Country) %>% addTiles() %>%   
      fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
      addMarkers(lng = -71.03 , lat = 42.37, popup = paste("<b>", "Boston", "</b><br>",
                                                           actionLink(inputId = "View", 
                                                                      label = "View Details", 
                                                                      onclick = 'Shiny.onInputChange(\"link_click_Site\",  Math.random())')))
  })

  observeEvent(input$link_click_Site, {
    output$Check <- renderText("Success")

  })

  observeEvent(input$Back, {
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click_Site")
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click")
  })

}


shinyApp(ui =ui, server = server)

回答1:


The same code that is used to reset your view when you click on the "Back" button can be used to reset the view whenever panels are switched.

You just have to give your sidebar an id, and then you can listen to this id's input, which tells you which panel is active. The solution below is a minimal fix with just two additions: sidebarMenu gets an input id, id = "mySidebar" and the observeEvent gets to observe a second variable input$mySidebar.

library(shiny)
library(shinydashboard)
library(maps)
library(leaflet)

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(sidebarMenu(id = "mySidebar",
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Widgets", tabName = "widgets", icon = icon("th"))
  )),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
      tags$script("
                  Shiny.addCustomMessageHandler('resetInputValue', function(variableName){
                  Shiny.onInputChange(variableName, null);
                  });
                  "),

      conditionalPanel(
        condition <- "input.link_click  === undefined || input.link_click === null",
        leafletOutput("Map", width = 1000, height = 500)

      ),


      conditionalPanel(
        condition <- "(input.link_click_Site  === undefined || input.link_click_Site === null) && (input.link_click  !== undefined && input.link_click !== null)",

        leafletOutput("CountryMap", width = 1000, height = 500)

      ),
      conditionalPanel(
        condition <- "(input.link_click_Site  !== undefined && input.link_click_Site !== null)",

        h3("Plots related to site chosen"),
        textOutput(outputId = "Check"),
        actionButton("Back", "Back")
      )
     ),
     tabItem(tabName = "widgets",
             h3("This is widget page")

             )
     )
  )
)


server <- function(input, output, session){
  Country = map("world", fill = TRUE, plot = FALSE, regions="USA")
  output$Map <- renderLeaflet({
    leaflet(Country) %>% addTiles() %>%  setView(0, 0,  zoom = 2)%>%
      #leaflet(target) %>% addTiles() %>%
      addPolygons(fillOpacity = 0.6,
                  fillColor = 'blue',
                  smoothFactor = 0.5, stroke = TRUE, weight = 1, popup =  paste("<b>", "USA", "</b><br>",
                                                                                actionLink(inputId = "View", 
                                                                                           label = "View Details", 
                                                                                           onclick = 'Shiny.onInputChange(\"link_click\",  Math.random())')))
  })

  output$CountryMap <- renderLeaflet({

    leaflet(Country) %>% addTiles() %>%   
      fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
      addMarkers(lng = -71.03 , lat = 42.37, popup = paste("<b>", "Boston", "</b><br>",
                                                           actionLink(inputId = "View", 
                                                                      label = "View Details", 
                                                                      onclick = 'Shiny.onInputChange(\"link_click_Site\",  Math.random())')))
  })

  observeEvent(input$link_click_Site, {
    output$Check <- renderText("Success")

  })

  observeEvent({input$Back; input$mySidebar} , {
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click_Site")
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click")
  })

}


shinyApp(ui =ui, server = server)


来源:https://stackoverflow.com/questions/37883046/shiny-dashboard-reset-the-conditional-panel-state-when-we-navigate-across-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!