[英]Shiny Dashboard: Reset the conditional panel state when we navigate across different tabitems
我有以下代碼,其中儀表板上有各種條件面板。 如果我們從一個條件面板導航到儀表板中的下一個,然后轉到小部件,最后返回儀表板,則該儀表板處於先前狀態。 當我從另一個選項卡面板返回時,我希望儀表板刷新(重置為原始狀態)。 有可能這樣做嗎?
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)
每當切換面板時,都可以使用與單擊“后退”按鈕時重置視圖相同的代碼來重置視圖。
您只需為側邊欄提供一個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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.