2016-06-17 86 views
0

我有以下代碼,其中儀表板中有各種條件面板。如果我們從儀表板中的一個條件面板導航到下一個,然後轉到小部件並最終返回儀表板,則儀表板處於之前的狀態。當我從另一個選項卡面板返回時,我希望儀表板刷新(重置爲原始狀態)。有可能做到這一點嗎?閃亮的儀表板:當我們瀏覽不同的Tabite時重置條件面板狀態

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

用來複位當你點擊「返回」按鈕,您的看法相同的代碼可被用於重置每當面板切換視圖。

你只需要給你的側邊欄id,然後你可以聽這個ID的輸入,它告訴你哪個面板是活動的。下面的解決方案是一個最小的修復,只需添加兩個: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) 
+0

這個工程,當我去的小部件選項卡,然後回到儀表板選項卡。當我在該選項卡中並且單擊儀表板選項卡項時,儀表盤選項卡是否會重置? – SBista

+0

@SBista如果我理解正確,您希望** Dashboard Tab **按鈕的功能類似於** Back **按鈕。這可以通過多種方式完成。最好的是** all **側邊欄按鈕的onclick事件。我會在今天下午晚些時候發佈解決方案。 –

相關問題