shosaco / vistime

Pretty timelines in R.
https://shosaco.github.io/vistime
GNU General Public License v3.0
168 stars 11 forks source link

Pass source and customdata to plot #34

Closed jonocarroll closed 3 months ago

jonocarroll commented 1 year ago

I would like to be able to interact with clicks on a vistime plot used in shiny. By default, plotly sets the source to "A" so this is possible. With several plots combined (aligned via plotly::subplot() with a shared X axis) the distinction between clicks on any one vistime plot is lost. By providing distinct sources these can be listened to independently when not combined. By providing distinct customdata the plots can be re-identified even when combined, despite having many tracks in each.

A small example app would be

library(plotly)
library(shiny)
library(vistime)

ui <- basicPage("",
                mainPanel(plotlyOutput("p"),
                          verbatimTextOutput("out")))

server <- function(input, output, session) {
  data1 <- read.csv(
    text = "event,start,end
Phase 1,2020-12-15,2020-12-24
Phase 2,2020-12-23,2020-12-23
Phase 3,2020-12-28,2021-01-06
Phase 4,2021-01-06,2021-02-02"
  )

  data2 <- read.csv(
    text = "event,start,end
Phase 5,2020-12-18,2020-12-22
Phase 6,2020-12-28,2021-01-05
Phase 7,2021-01-12,2021-01-16
Phase 8,2021-01-22,2021-02-24"
  )

  output$p <- renderPlotly({
    p1 <- vistime(data1, customdata = "plot1")
    p2 <- vistime(data2, customdata = "plot2")

    s <- subplot(
      list(p1, p2),
      shareX = TRUE,
      nrows = 2,
      heights = c(0.5, 0.5)
    ) |>
      event_register("plotly_click")
    s$x$source <- "click_src"
    s
  })

  output$out <- renderPrint({
    click_data <- event_data("plotly_click", source = "click_src")
    req(click_data)
    message("CLICK!")
    click_data
  })
}

runApp(shinyApp(ui = ui, server = server))

https://github.com/shosaco/vistime/assets/9496865/0baa0d03-73da-44b1-9977-f518a0d8f9ba

codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 75.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 86.04%. Comparing base (8a3a5cb) to head (d2a82c5).

:exclamation: Current head d2a82c5 differs from pull request most recent head c35bca7. Consider uploading reports for the commit c35bca7 to get more accurate results

Files Patch % Lines
R/plot_plotly.R 66.66% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #34 +/- ## ========================================== - Coverage 86.28% 86.04% -0.25% ========================================== Files 12 12 Lines 299 301 +2 ========================================== + Hits 258 259 +1 - Misses 41 42 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

shosaco commented 3 months ago

Thanks a lot for that addition!