rshinycrosstalk

Select multiple values and unselect certain values of dataframe using crosstalk


I have the shiny app below in which I use crosstalk package to create interaction between the chart and the table. I would like to ask if it is possible to select more than one bars at the same time in order to bring back the table to its initial form and also how can you unselect a bar without having to click on another one?

library(shiny)
library(ggplot2)
library(plotly)
library(DT)
library(crosstalk)

ui <- fluidPage(
  plotlyOutput("plt"),
  DT::dataTableOutput("dt")
)

server <- function(input, output) {
  df <- data.frame(dose=c("D0.5", "D1", "D2"),
                   len=c(4.2, 10, 29.5))
  df2 <- data.frame(dose=c("D0.5", "D1", "D2"),
                    siz=c(2, 10, 2.5))
  
  shared_df <- SharedData$new(df, key = ~dose, group = "group")
  shared_df2 <- SharedData$new(df2, key = ~dose, group = "group")
  
  output$plt<-renderPlotly({
    # Basic barplot
    p <- ggplot(data=shared_df, aes(x=dose, y=len)) +
      geom_bar(stat="identity")
    ggplotly(p)
  })
  
  output$dt<-DT::renderDataTable({
    shared_df2
  }, server = FALSE)
}
shinyApp(ui, server)

Solution

  • When only one bar is selected and one row is displayed in the table, press and hold shift-key and click on other unselected bar to select it and the associated data row is displayed in the table. Once all bars are selected, all rows are displayed.

    Also, you can select and unselect anywhere in the table to display bars that are selected/unselected according to the data row selected/unselected.