在Shiny中用Crosstalk实现D3Scatter筛选DT表格行的问题
Hey there, I’ve run into this exact issue before—crosstalk works perfectly outside Shiny but breaks the brush-to-table direction when you move it into a Shiny app. Let’s break down why this happens and how to fix it.
The Root Cause
When you use DT in Shiny, it defaults to server-side processing to handle large datasets efficiently. But crosstalk’s client-side filtering relies on DT running in client-side mode. That’s why you can still select rows in the table to highlight scatter points (server-side can send selections to the client), but brushing the scatter plot can’t filter the table (client-side events can’t trigger server-side DT updates).
The Fix
All you need to do is force DT to use client-side processing by adding server = FALSE to your datatable() call. Here’s your corrected code:
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({ # Enable client-side processing for crosstalk compatibility datatable(shared_iris, server = FALSE) }) } shinyApp(ui, server)
Additional Notes
- If you’re working with a very large dataset, client-side processing might slow things down, but for datasets like
irisit’s totally fine. - Make sure you’re running the latest versions of
crosstalk,d3scatter, andDT—older versions had some compatibility quirks with Shiny.
内容的提问来源于stack exchange,提问作者Chris Beeley

