You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Shiny中用Crosstalk实现D3Scatter筛选DT表格行的问题

Fix: D3Scatter Brush-to-DT Filtering in Shiny

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 iris it’s totally fine.
  • Make sure you’re running the latest versions of crosstalk, d3scatter, and DT—older versions had some compatibility quirks with Shiny.

内容的提问来源于stack exchange,提问作者Chris Beeley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:08:40