如何用actionButton重置Shiny中plotly点击子集化的图表至初始状态
实现Shiny应用的图表重置功能
要给你的Shiny应用添加重置按钮,让所有图表恢复初始显示状态,需要通过统一管理选中状态的方式实现,具体修改如下:
核心修改点
- 新增
selected_date这个reactiveVal来存储用户点击选中的日期,初始值为NULL - 监听三个图表的点击事件,更新
selected_date的值 - 重置按钮点击时,将
selected_date设为NULL,触发所有图表重新加载完整数据 - 调整三个图表的渲染逻辑,基于
selected_date筛选数据,替代原来直接判断event_data的逻辑
修改后的完整代码
library(shiny) library(shinydashboard) library(plotly) library(dplyr) library(ggplot2) library(bupaR) pr59<-structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689", "WC4121068", "WC4120667", "WC4120666", "WC4120667", "WC4121068", "WC4120667", "WC4121068"), lifecycle = c(110, 110, 110, 110, 120, 110, 130, 120, 10, 130), action = c("WC4120721-CN354877", "WC4120667-CN354878", "WC4120689-CN356752", "WC4121068-CN301950", "WC4120667-CSW310", "WC4120666-CN354878", "WC4120667-CSW308", "WC4121068-CSW303", "WC4120667-CSW309", "WC4121068-CSW308"), activity = c("Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "BBH-1&2", "Forged Wire, Medium (Sport)", "TCE Cleaning", "SOLO Oil", "Tempering", "TCE Cleaning"), resource = c("3419", "3216", "3409", "3201", "C3-100", "3216", "C3-080", "C3-030", "C3-090", "C3-080"), timestamp = structure(c(1606964400, 1607115480, 1607435760, 1607568120, 1607630220, 1607670780, 1607685420, 1607710800, 1607729520, 1607744100), tzone = "", class = c("POSIXct", "POSIXt")), .order = 1:10), row.names = c(NA, -10L), class = c("eventlog", "log", "tbl_df", "tbl", "data.frame"), spec = structure(list( cols = list(case_id = structure(list(), class = c("collector_character", "collector")), lifecycle = structure(list(), class = c("collector_double", "collector")), action = structure(list(), class = c("collector_character", "collector")), activity = structure(list(), class = c("collector_character", "collector")), resource = structure(list(), class = c("collector_character", "collector")), timestamp = structure(list(), class = c("collector_character", "collector"))), default = structure(list(), class = c("collector_guess", "collector")), delim = ";"), class = "col_spec"), case_id = "case_id", activity_id = "activity", activity_instance_id = "action", lifecycle_id = "lifecycle", resource_id = "resource", timestamp = "timestamp") ui <- tags$body( dashboardPage( header = dashboardHeader(), sidebar = dashboardSidebar( actionButton("res","RESET") ), body = dashboardBody( plotlyOutput("plot1"), plotlyOutput("plot2"), plotlyOutput("plot3") ) ) ) server <- function(input, output, session) { # 用reactiveVal统一管理选中的日期状态 selected_date <- reactiveVal(NULL) # 监听三个图表的点击事件,更新选中日期 observeEvent(event_data("plotly_click", source = "myPlotSource1"), { selected_date(as.Date(event_data("plotly_click", source = "myPlotSource1")$customdata)) }) observeEvent(event_data("plotly_click", source = "myPlotSource2"), { selected_date(as.Date(event_data("plotly_click", source = "myPlotSource2")$customdata)) }) observeEvent(event_data("plotly_click", source = "myPlotSource3"), { selected_date(as.Date(event_data("plotly_click", source = "myPlotSource3")$customdata)) }) # 重置按钮点击事件:清空选中日期 observeEvent(input$res, { selected_date(NULL) }) output$plot1 <- renderPlotly({ # 根据选中日期筛选数据 displaydat <- if (!is.null(selected_date())) { subset(pr59, as.Date(timestamp) == selected_date()) } else { pr59 } dat <- displaydat |> group_by(date = as.Date(timestamp)) |> bupaR::n_cases() p <- ggplot(data = dat, aes(x = date, y = n_cases, customdata = date)) + geom_area(fill = "#69b3a2", alpha = 0.4) + geom_line(color = "#69b3a2", size = 0.5) + geom_point(size = 1, color = "#69b3a2") + scale_color_grey() + theme_classic() + labs(title = "Cases per month", x = "timestamp", y = "Cases") ggplotly(p, source = "myPlotSource1") }) output$plot2 <- renderPlotly({ displaydat <- if (!is.null(selected_date())) { subset(pr59, as.Date(timestamp) == selected_date()) } else { pr59 } dat <- displaydat|> group_by(date = as.Date(timestamp)) |> bupaR::n_cases() p <- ggplot(data = dat, aes(x = date, y = n_cases, customdata = date)) + geom_area(fill = "#69b3a2", alpha = 0.4) + geom_line(color = "#69b3a2", size = 0.5) + geom_point(size = 1, color = "#69b3a2") + scale_color_grey() + theme_classic() + labs(title = "Cases per month", x = "timestamp", y = "events") ggplotly(p, source = "myPlotSource2") }) output$plot3 <- renderPlotly({ displaydat <- if (!is.null(selected_date())) { subset(pr59, as.Date(timestamp) == selected_date()) } else { pr59 } dat <- displaydat |> group_by(date = as.Date(timestamp)) |> bupaR::n_cases() p <- ggplot(data = dat, aes(x =date, y = n_cases, customdata = date)) + geom_area(fill = "#69b3a2", alpha = 0.4) + geom_line(color = "#69b3a2", size = 0.5) + geom_point(size = 1, color = "#69b3a2") + scale_color_grey() + theme_classic() + labs(title = "Cases per month", x = "timestamp", y = "objects") ggplotly(p, source = "myPlotSource3") }) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者firmo23
相关产品推荐
相关产品推荐

