shosaco / vistime

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

Hover information #10

Closed mohammedfahim1 closed 4 years ago

mohammedfahim1 commented 4 years ago

Hi , I am trying to create a shiny app with the package and I see that tooltip can be used to display information when hovered.

I was wondering if it is possible to send a key or a hoverinfo through plotly_hover in event data?

Example: In the data set you have given press:

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

Say I add rownumber for each row: pres$id<-rowname(pres)

and I would like to use this data to hover over and display more information about that record in shiny example :

ui <- fluidPage(
    plotlyOutput("plot"),
    verbatimTextOutput("event")
)
server <- function(input, output) {
#Plotting
    output$plot <- renderPlotly({
        vistime(press,events="Position",groups ="Name",show_labels = FALSE)
        })
#Capturing the event
    output$event <- renderPrint({
#Hovering
        d <- event_data("plotly_hover")
        if (is.null(d)) "Hover on a point!" else d    })
}    

d as an event only has curvenumber , pointnumber (which is not unique) ,x and y but I would like to do something similar to :

 output$event <- renderPrint({
        d <- event_data("plotly_hover")
        if (is.null(d)) "Hover on a point!" else 
               press %>% 
                        filter( id == d$key) %>% 
                        pull(fontcolor) 

Any Help would be much appreciated.

mohammedfahim1 commented 4 years ago

I figured out a way.

shosaco commented 4 years ago

Please share your solution for other people having the same problem. Thanks!

mohammedfahim1 commented 4 years ago

Sorry for the delay , As a temporary fix : Inside the package: plot_ly has a key in its function call. So in the plot_events function and in the plot_range function inside the library I added key= group in the add_marker and then returned the data from the function as

df<<- data %>% group_by(group) %>% 
      mutate(pointNumber = row_number()-1)  

Server side :

edata<- event_data("plotly_hover")

df %>% 
  filter(start == edata[,x ] & (start==edata[,x] | end==edata[,y]) & group==edata[,key])

There might be a better way of doing this , This just works for me.

Thank You