简体   繁体   English

Shiny Dashboard:在不同的Tabitem中导航时,重置条件面板状态

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

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)

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. 您只需为侧边栏提供一个id ,然后您就可以收听此ID的输入,它告诉您哪个面板处于活动状态。 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 . 下面的解决方案是一个最小的解决方案,仅增加了两个: sidebarMenu获取一个输入id, id = "mySidebar" ,而observeEvent观察第二个变量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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM