rplotlydt

Client-side linking of plotly chart and DT table


I am trying to adapt an example from Carson Sievert's plotly book, where he links a leaflet map to a data-frame, so that it works for a plotly chart instead

 library(plotly)
 library(crosstalk)
 library(sf)

 nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
 df <- nc %>% 
     select(AREA,NAME) %>% 
     group_by(NAME)

 st_geometry(df) <- NULL

 hk <- highlight_key(df)

chart <-plot_ly(df, y= ~NAME,x= ~AREA, split = ~NAME) %>%
       highlight() %>%
       layout(showlegend = FALSE) 

  bscols(chart, DT::datatable(hk))

This produces the required chart and data for every row, but clicking on a value on the chart does not filter the data-table


Solution

  • You need to provide hk to both the plotly chart and the dt table. Also, I'd recommend adding layout(barmode = "overlay") the bar chart definition