Shiny Dashboard:当我们在不同的 tabitem 之间导航时重置条件面板状态
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)
单击 "Back" 按钮时用于重置视图的相同代码可用于在面板切换时重置视图。
你只需要给你的侧边栏一个 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)
我有以下代码,其中仪表板中有各种条件面板。如果我们从一个条件面板导航到仪表板中的下一个,然后转到小部件并最终返回到仪表板,则仪表板处于之前的状态。当我从另一个选项卡面板返回时,我希望刷新仪表板(重置为原始状态)。可以这样做吗?
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)
单击 "Back" 按钮时用于重置视图的相同代码可用于在面板切换时重置视图。
你只需要给你的侧边栏一个 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)