You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过selectInput()实现process_map()多边的隐藏与显示

解决Shiny流程地图多选过渡隐藏问题

下面是修正后的代码,支持多选过渡并隐藏对应的边,同时保留单选和"显示全部"的功能:

library(shiny)
library(bupaR)
library(svgPanZoom)
library(DiagrammeRsvg)
library(DiagrammeR)
library(processmapR)

edges <- patients %>% process_map(performance(mean, "days"))
edges <- attr(edges, "edges")
colnames(edges)[1] <- "predecessor"
colnames(edges)[2] <- "successor"

graph <- process_map(patients
                     , type_nodes = frequency("absolute",color_scale = "Greys")
                     , type_edges = frequency("absolute",color_edges = "Greys"),
                     rankdir = "LR", render = FALSE)

ui <- shinyUI(fluidPage(
  selectInput("tran","transitions"
              , choices = c("All", paste(edges$predecessor,"-",edges$successor)),
              multiple = TRUE
              , selected = "All"),
  svgPanZoomOutput("pmap", height = 500, width = 1600)
  
))
server <- function(input, output) {
  
  output$pmap <- renderSvgPanZoom({
    req(input$tran)
    
    # 若选中"All",直接使用原始完整图
    if ("All" %in% input$tran) {
      newg <- graph
    } else {
      # 创建原始图的副本用于修改
      ndf <- get_node_df(graph)
      edf <- get_edge_df(graph)
      newg <- create_graph(nodes_df = ndf, edges_df = edf)
      newg$global_attrs <- graph$global_attrs
      
      # 遍历所有选中的过渡,逐个删除对应边
      for (tran in input$tran) {
        pre <- strsplit(tran, " - ")[[1]][[1]]
        suc <- strsplit(tran, " - ")[[1]][[2]]
        
        # 匹配对应前驱和后继节点
        from_nodes <- newg %>% clear_selection() %>% 
          select_nodes(conditions = startsWith(tooltip, pre)) %>% get_selection()
        to_nodes <- newg %>% clear_selection() %>% 
          select_nodes(conditions = startsWith(tooltip, suc)) %>% get_selection()
        
        # 删除选中的边
        newg <- newg %>% clear_selection() %>% 
          select_edges(from = from_nodes, to = to_nodes) %>% delete_edges_ws()
      }
    }
    
    newg %>% generate_dot() %>% grViz(width = 1000, height = 2000) %>% 
      export_svg %>% svgPanZoom(height=800, controlIconsEnabled = TRUE)
  })
  
}
shinyApp(ui=ui,server=server)

关键修改说明

  • 多选功能启用:打开selectInput的multiple=TRUE参数,允许同时选择多个过渡。
  • 逻辑判断优化:用"All" %in% input$tran替代原有的input$tran != "All",避免多选时向量逻辑判断的错误,确保只要选中"All"就显示完整流程。
  • 多选循环处理:当未选中"All"时,遍历所有选中的过渡项,逐个匹配并删除对应的边,实现批量隐藏选中过渡的效果。
  • 状态保持:每次删除边时基于当前修改后的图对象操作,确保多步删除的状态正确传递。

内容的提问来源于stack exchange,提问作者firmo23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:50:28