I have the shiny
app below in which I create a process map. What I want to do is subset this process map based on the transitions
selectInput()
.
All the transitions can be seen from the obect edges
which I extract from the process_map()
object at the beginning but then how can I pass the selected from the selectInput()
again to the process_map()
object?what I acually need is to hide/display the edges between the nodes if deselect/select one transition pair
library(shiny)
library(bupaR)
library(svgPanZoom)
library(DiagrammeR)
library(DiagrammeRsvg)
library(processmapR)
edges<-patients %>%
process_map(performance(mean, "days"))
edges <- attr(edges, "edges")
colnames(edges)[1]<-"predecessor"
colnames(edges)[2]<-"successor"
ui <-shinyUI(fluidPage(
selectInput("tran","transitions",choices = paste(edges$predecessor,"-",edges$successor),
selected = paste(edges$predecessor,"-",edges$successor),multiple=T),
svgPanZoomOutput("pmap",height = 500,width = 1600)
))
server <- function(input, output) {
output$pmap <- renderSvgPanZoom({
process_map(patients, type_nodes = frequency("absolute",color_scale = "Greys"),type_edges = frequency("absolute",color_edges = "Greys"),
rankdir = "LR", render = FALSE) %>%
generate_dot() %>%
grViz(width = 1000, height = 2000) %>%
export_svg %>%
svgPanZoom(height=800, controlIconsEnabled = TRUE)
})
}
shinyApp(ui=ui,server=server)
EDIT
With new understanding that the goal is to filter out a specific transition/edge, one way of doing it is to manipulate the graph as I couldn't really understand process_map
documentation. DiagrammeR` documentation was much better http://rich-iannone.github.io/DiagrammeR/docs.html
Running example below. Notice that the main graph is built once, and after selecting a specific filter it is duplicated, and manipulated (finding and removing the edge).
With this example you should be able adjust to your specific case (use RStudio debugging for inspecting variables, and so on).
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))
,selected = "All"),
svgPanZoomOutput("pmap",height = 500,width = 1600)
))
server <- function(input, output) {
output$pmap <- renderSvgPanZoom({
req(input$tran)
if (input$tran != "All"){
pre <- strsplit(input$tran, " - ")[[1]][[1]]
suc <- strsplit(input$tran, " - ")[[1]][[2]]
#creating copy of graph for processing
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
#Finding edges to remove based on pre/suc nodes, selecting edge, removing
#using startWith due termination chars being added
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 %>% render_graph # debugging
} else {
newg <- graph
}
newg %>% generate_dot() %>% grViz(width = 1000, height = 2000) %>%
export_svg %>% svgPanZoom(height=800, controlIconsEnabled = TRUE)
})
}
shinyApp(ui=ui,server=server)
Can't really tell what exactly you want to achieve, but the general idea is that you need to use the selected transition, represented by input$tran
, to filter the data.
My naive guess to filter the raw source data would be something like this:
output$pmap <- renderSvgPanZoom({
req(input$tran)
pre <- strsplit(input$tran, " - ")[[1]][[1]]
suc <- strsplit(input$tran, " - ")[[1]][[2]]
p <- patients[which(patients$handling==pre | patients$handling==suc),]
process_map(p, type_nodes = frequency("absolute",color_scale = "Greys")
,type_edges = frequency("absolute",color_edges = "Greys"),
rankdir = "LR", render = FALSE) %>% generate_dot() %>%
grViz(width = 1000, height = 2000) %>% export_svg %>%
svgPanZoom(height=800, controlIconsEnabled = TRUE)
})
Notice the parsing of the selected transition and later filtering the patients list.
This will consider only the nodes directly participating in the selected transition, which might not be what you need (e.g.: you could want to display 'inner-processes, but that means the current transition list is not adequate).
Other potential needs not easy to achieve with simple dataframe filtering would be to filter the process up to the selected transition (or all process after the actual transition until finishes). That would require filtering after the process_map
calculation (and I'm not in position to recommend how)