2016-06-17 6 views
0

Ich habe den folgenden Code, in dem verschiedene Bedingungsfenster im Dashboard vorhanden sind. Wenn wir von einem bedingten Feld zum nächsten im Dashboard navigieren und dann zum Widget gehen und schließlich zum Dashboard zurückkehren, befindet sich das Dashboard im vorherigen Status. Ich möchte, dass das Dashboard aktualisiert wird (auf den ursprünglichen Zustand zurückgesetzt wird), wenn ich von einer anderen Registerkarte zurückkomme. Ist das möglich?Shiny Dashboard: Setzen Sie den Zustand der bedingten Anzeige zurück, wenn wir zwischen verschiedenen Tabitems navigieren

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) 

Antwort

1

Der gleiche Code, der Ihrer Ansicht zurückgesetzt wird verwendet, wenn Sie auf die Schaltfläche „Zurück“ klicken, um die Ansicht, wenn Platten eingeschaltet zurückgesetzt werden.

Sie müssen nur Ihre Sidebar id geben, und dann können Sie diese ID-Eingabe hören, die Ihnen sagt, welches Panel aktiv ist. Die folgende Lösung ist ein minimaler Fix mit nur zwei Ergänzungen: sidebarMenu bekommt eine Eingabe-ID, id = "mySidebar" und die observeEvent bekommt, um eine zweite Variable zu beobachten 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) 
+0

Das funktioniert, wenn ich auf Widget-Registerkarte gehe und dann zurück zum Dashboard Registerkarte. Gibt es eine Möglichkeit, wie die Registerkarte "Dashboard" zurückgesetzt wird, wenn ich auf dieser Registerkarte bin und auf den Tab "Dashboard" klicke? – SBista

+0

@SBista Wenn ich das richtig verstanden habe, soll die ** Dashboard Tab ** Taste wie die ** Back ** Taste funktionieren. Das kann auf verschiedene Arten geschehen. Am besten wäre ein Onclick-Event für ** alle ** Sidebar-Buttons. Ich werde später am Nachmittag eine Lösung veröffentlichen. –

Verwandte Themen