Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows from a DT table using Crosstalk in Shiny

Tags:

dt

shiny

I confess, I did post this question over on RStudio three days ago but it has not had enough love yet, so I'm trying again here. I hope that's okay. The original question is here (the text is the same in both, I'm just being transparent). https://community.rstudio.com/t/selecting-rows-from-a-dt-table-using-crosstalk-in-shiny/4079

So I would like to brush across points in D3Scatter and use it to filter the rows of a datatable produced using the DT package with crosstalk.

Just like this, which totally works outside of shiny:

library(crosstalk)
library(d3scatter)
library(DT)

shared_iris <- SharedData$new(iris)

bscols(d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width = "100%",
             x_lim = range(iris$Petal.Length), y_lim = range(iris$Petal.Width)),

   datatable(shared_iris))

But when I put it in Shiny, I can select points on the scatter from the table, but not vice versa:

library(shiny)
library(crosstalk)
library(d3scatter)
library(DT)

ui <- fluidPage(
  fluidRow(
    column(6, d3scatterOutput("scatter1")),
    column(6, DT::dataTableOutput("scatter2"))
  )
)

server <- function(input, output, session) {
  jittered_iris <- reactive({
    iris
  })

  shared_iris <- SharedData$new(jittered_iris)

  output$scatter1 <- renderD3scatter({
    d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width = "100%",
          x_lim = range(iris$Petal.Length), y_lim = range(iris$Petal.Width))
  })

  output$scatter2 <- DT::renderDataTable({
    datatable(shared_iris)
  })
}

shinyApp(ui, server)

They’ve got it working here: https://rstudio-pubs-static.s3.amazonaws.com/215948_95c1ab86ad334d2f82856d9e5ebc16af.html

I’m at a loss. I feel like I’ve tried everything. Any clues anyone?

Thanks,

like image 561
Chris Beeley Avatar asked Oct 12 '25 08:10

Chris Beeley


1 Answers

Crosstalk integration in DT only works with client-side processing . Try DT::renderDataTable with server = FALSE

library(shiny)
library(crosstalk)
library(d3scatter)
library(DT)

ui <- fluidPage(
  fluidRow(
    column(6, d3scatterOutput("scatter1")),
    column(6, DT::dataTableOutput("scatter2"))
  )
)

server <- function(input, output, session) {
  jittered_iris <- reactive({
    iris
  })

  shared_iris <- SharedData$new(jittered_iris)

  output$scatter1 <- renderD3scatter({
    d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width = "100%",
              x_lim = range(iris$Petal.Length), y_lim = range(iris$Petal.Width))
  })

  output$scatter2 <- DT::renderDataTable({
    datatable(shared_iris)
  }, server = FALSE)
}

shinyApp(ui, server)

DT should throw an error when using Crosstalk with server-side processing

Error in widgetFunc: Crosstalk only works with DT client mode: DT::renderDataTable({...}, server=FALSE)

but I think that broke here: https://github.com/rstudio/DT/commit/893708ca10def9cfe0733598019b62a8230fc52b

Guess I can file an issue on this if no one else has.

like image 184
greg L Avatar answered Oct 15 '25 00:10

greg L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!