我有以下代码,其中仪表板中有各种条件面板。如果我们从仪表板中的一个条件面板导航到下一个,然后转到窗口小部件并最终返回仪表板,则仪表板处于先前状态。当我从另一个选项卡面板返回时,我希望刷新仪表板(重置为原始状态)。有可能这样做吗?
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)
答案 0 :(得分: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)