闪亮的仪表板:当我们在不同的选项卡项之间导航时,重置条件面板状态 [英] Shiny Dashboard: Reset the conditional panel state when we navigate across different tabitems
本文介绍了闪亮的仪表板:当我们在不同的选项卡项之间导航时,重置条件面板状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,其中仪表板中有各种条件面板。如果我们在Dashboard中从一个条件面板导航到下一个,然后转到Widget,最后返回到Dashboard,则仪表板处于以前的状态。当我从另一个选项卡面板返回时,我希望刷新仪表板(重置为原始状态)。可以做到这一点吗?
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"
获取第二个变量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)
这篇关于闪亮的仪表板:当我们在不同的选项卡项之间导航时,重置条件面板状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文