✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

R Shiny plotly DT table interaction

I want to do table-plot interaction using R-Shiny and Plotly.

  1. After click any point in plot: I want to increase the size of point in graph, color it red also in the table bring corresponding row at top of the table and highlight the row.
  2. Similarly, after clicking (single) row in the table, corresponding point in the plot should be highlighted in red and bigger in size.

As my data is big I need to do this on serve side.

I was able to create the plot and table below is my code.

ui.R file:

ui <- fluidPage(
  fluidPage(column(width = 6, plotlyOutput("volcanoplot", height = 350))),
  fluidPage(DT::dataTableOutput("de_table"))
  )

And server.R file:

library("DT")
library(plotly)
library(shiny)

m <- mtcars[, c("mpg", "wt", "disp")] %>% 
  tibble::rownames_to_column()

function(input, output, session) {
  shared_data <- SharedData$new(m, ~rowname)

  output$volcanoplot <- renderPlotly({

        pp <- shared_data %>% plot_ly(source = 'volcanoplot') %>%
          add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = "markers")
      })

      # highlight selected rows in the table
      output$de_table <- DT::renderDataTable({
        dt <- DT::datatable(shared_data$data() , selection = 'single', rownames= FALSE)})
}

I was able to understand the click and table select variables as,

click_detect = plotly::event_data('plotly_click', source = 'volcanoplot')
s <- input$de_table_rows_selected 

Thanks for the help.

FYI: I tried to get help from https://stackoverflow.com/questions/60319505/r-shiny-plotly-dt-table-interaction
But was not useful.