title: Selection of activity trace in a chart and display in a data table in R shiny
body: <p>If you run the R shiny script below, you get two boxes in an R shiny dashboard, The chart on the left displays a plot for all the traces or set of activities that occur in the eventlog data "patients_eventlog". "patients2" is a data in the script that explains each and every case appearing in column "a1", and corresponding activities basides in column "a2". My requirement is that when I click anywhere on a particular trace in the chart on left, I should get the relevant columns "a1","a2" and "a3" with the data having only and only those cases in which the activities in that trace are occurring. E.g. Let's say 
  a trace in the chart on left has activites "Registration" and "Triage and Assessment", the by clicking on the trace, I want to see the cases with only and only those two activities. This just needs a minor tweak in the "output$sankey_table" server component. Please help and thanks.</p>
  
  <pre><code>## app.R ##
  library(shiny)
  library(shinydashboard)
  library(bupaR)
  library(lubridate)
  library(dplyr)
  library(xml2)
  library(ggplot2)
  library(ggthemes)
  library(glue)
  library(tibble)
  library(miniUI)
  library(tidyr)
  library(shinyWidgets)
  library(plotly)
  library(DT)
  library(splitstackshape)
  library(scales)
  patients$patient = as.character(patients$patient)
  a1 = patients$patient
  a2 = patients$handling
  a3 = patients$time
  a123 = data.frame(a1,a2,a3)
  patients_eventlog = simple_eventlog(a123, case_id = "a1",activity_id = "a2", 
  timestamp = "a3")
  dta &lt;- reactive({
  tr &lt;- data.frame(traces(patients_eventlog, output_traces = T, output_cases = 
  F))
  tr.df &lt;- cSplit(tr, "trace", ",")
  tr.df$af_percent &lt;-
  percent(tr.df$absolute_frequency/sum(tr.df$absolute_frequency))
  pos &lt;- c(1,4:ncol(tr.df))
  tr.df &lt;- tr.df[,..pos]
  tr.df &lt;- melt(tr.df, id.vars = c("trace_id","af_percent"))
  tr.df
  })
  Purchase_Final &lt;- reactive({
  patients1 &lt;- arrange(patients_eventlog, a1)
  patients2 &lt;- patients1 %&gt;% arrange(a1, a3,a2)
  patients2 %&gt;%
  group_by(a1) %&gt;%
  mutate(a3 = as.POSIXct(a3, format = "%m/%d/%Y %H:%M"),diff_in_sec = a3 - 
  lag(a3)) %&gt;% 
  mutate(diff_in_sec = ifelse(is.na(diff_in_sec),0,diff_in_sec)) %&gt;% 
  mutate(diff_in_hours = as.numeric(diff_in_sec/3600)) %&gt;% 
  mutate(diff_in_days = as.numeric(diff_in_hours/24))
  })
  ui &lt;- dashboardPage(
  dashboardHeader(title = "Trace Chart"),
  dashboardSidebar(
  width = 0
  ),
  dashboardBody(
  box(title = "Trace Chart", status = "primary",height = "455" ,solidHeader = 
  T,
      plotlyOutput("trace_plot"),style = "height:420px; overflow-y: 
  scroll;overflow-x: scroll;"),
  
  box( title = "Trace Summary", status = "primary", height = "455",solidHeader 
  = T, 
       dataTableOutput("sankey_table"))
  )
  )
  server &lt;- function(input, output) 
  { 
  output$trace_plot &lt;- renderPlotly({
  
  
    mp1 = ggplot(data = dta(), aes(x = variable,y = trace_id, fill = value,
                                 label = value,
                                 text=paste("Variable:",variable,"&lt;br&gt; Trace 
                                            ID:",trace_id,"&lt;br&gt; 
   Value:",value,"&lt;br&gt; Actuals:",af_percent))) +
    geom_tile(colour = "white") +
    geom_text(colour = "white", fontface = "bold", size = 2) +
    scale_fill_discrete(na.value="transparent") +
    theme(legend.position="none") + labs(x = "Traces", y = "Activities")
    ggplotly(mp1, tooltip=c("text"), height = 1226, width = 1205)
  
   })
   output$sankey_table &lt;- renderDataTable({
   d = event_data("plotly_click")
   d
   })
  
   }
   shinyApp(ui, server)
  </code></pre>
  
  <h1>Addon Script for reference</h1>
  
  <h2>app.R</h2>
  
  <pre><code>library(shiny)
  library(shinydashboard)
  library(bupaR)
  library(lubridate)
  library(dplyr)
  library(xml2)
  library(ggplot2)
  library(ggthemes)
  library(glue)
  library(tibble)
  library(miniUI)
  library(tidyr)
  library(shinyWidgets)
  library(plotly)
  library(DT)
  library(splitstackshape)
  library(scales)
  dta &lt;- reactive({
  tr &lt;- data.frame(traces(patients, output_traces = T, output_cases = F))
  tr.df &lt;- cSplit(tr, "trace", ",")
  tr.df$af_percent &lt;-
  percent(tr.df$absolute_frequency/sum(tr.df$absolute_frequency))
  pos &lt;- c(1,4:ncol(tr.df))
  tr.df &lt;- tr.df[,..pos]
  tr.df &lt;- melt(tr.df, id.vars = c("trace_id","af_percent"))
  tr.df
  })
  patients10 &lt;- reactive({
  patients11 &lt;- arrange(patients, patient)
  patients12 &lt;- patients11 %&gt;% arrange(patient, time,handling_id)
  patients12 %&gt;%
  group_by(patient) %&gt;%
  mutate(time = as.POSIXct(time, format = "%m/%d/%Y %H:%M"),diff_in_sec = time 
  - lag(time)) %&gt;% 
  mutate(diff_in_sec = ifelse(is.na(diff_in_sec),0,diff_in_sec)) %&gt;% 
  mutate(diff_in_hours = as.numeric(diff_in_sec/3600)) %&gt;% 
  mutate(diff_in_days = as.numeric(diff_in_hours/24))
  })
  ui &lt;- dashboardPage(
  dashboardHeader(title = "Sankey Chart"),
  dashboardSidebar(
  width = 0
  ),
  dashboardBody(
  box(title = "Sankey Chart", status = "primary",height = "455" ,solidHeader = 
  T,
      plotlyOutput("trace_plot")),
  
  box( title = "Case Summary", status = "primary", height = "455",solidHeader 
  = T, 
       dataTableOutput("trace_table"))
  )
  )
  server &lt;- function(input, output) 
  { 
  output$trace_plot &lt;- renderPlotly({
  mp1 = ggplot(data = dta(), aes(x = variable,y = trace_id, fill = value,
                                 label = value,
                                 text=paste("Variable:",variable,"&lt;br&gt; Trace 
                                            ID:",trace_id,"&lt;br&gt; 
  Value:",value,"&lt;br&gt; Actuals:",af_percent))) +
    geom_tile(colour = "white") +
    geom_text(colour = "white", fontface = "bold", size = 2) +
    scale_fill_discrete(na.value="transparent") +
    theme(legend.position="none") + labs(x = "Traces", y = "Activities")
  ggplotly(mp1, tooltip=c("text"), height = 516, width = 605)
  
  })
  output$trace_table &lt;- renderDataTable({
  req(event_data("plotly_click"))
  Values &lt;- dta() %&gt;% 
    filter(trace_id == event_data("plotly_click")[["y"]]) %&gt;% 
    select(value)
  
  valueText &lt;- paste0(Values[[1]] %&gt;% na.omit(),collapse = "")
  agg &lt;- aggregate(handling~patient, data = patients10(), FUN = function(y)
  {paste0(unique(y),collapse = "")})
  
  currentPatient &lt;- agg$patient[agg$handling == valueText]
  
  patients10_final &lt;- patients10() %&gt;%
    filter(patient %in% currentPatient)
  datatable(patients10_final, options = list(paging = FALSE, searching = 
  FALSE))
  })
  }
  shinyApp(ui, server)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1qaLx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1qaLx.png" alt="Trace Plot"></a></p>