plotly / plotly/plotly.R

Passing `selectedpoints` as an argument to a plot doesn't update the stored selected data.

未关闭
#2,238 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

Bug

This problem is also described here:
https://github.com/plotly/dash/issues/2301

This is true when constructing the graph with a selectedpoints argument or when restyling the graph with a selectedpoints argument.

I have been digging into the source code to try to figure out a workaround to this, but my limited knowledge of JavaScript is obscuring the solution.

Reproducible Example

Please see this reproducible example below:

library(shiny)


#' Render the mtcars graphs, wt x-axis, mpg y-axis
render_graph <- function() {
  plotly::plot_ly(
    mtcars,
    x = ~ wt,
    y = ~ mpg,
    selected = list(marker = list(color = "red")),
    hoverinfo = "text",
    text = rownames(mtcars),
    type = "scatter",
    mode = "markers",
    source = "graph"
  ) %>%
    plotly::layout(dragmode = "select",
                   clickmode = "event+select")
}


ui <- fluidPage(fluidRow(
  actionButton("action", "Print Selected"),
  plotly::plotlyOutput("graph"),
  DT::DTOutput("table")
))


server <- function(input, output, session) {
  output$table <- DT::renderDT({
    mtcars
  })
  
  output$graph <- plotly::renderPlotly(render_graph())
  
  plot_proxy <- plotly::plotlyProxy("graph")
  table_proxy <- DT::dataTableProxy("table")
  
  observeEvent(input$action, {
    selected <-
      isolate(plotly::event_data("plotly_selected", source = "graph"))
    print(selected)
  })
  
  selected_data <- reactive({
    selected <-
      plotly::event_data("plotly_selected", source = "graph")
    selected <- selected$pointNumber + 1
    
    selected
  })

  observe({
    table_proxy %>% DT::selectRows(selected_data())
  })

  # update the plot based on table
  observeEvent(input$table_cell_clicked, {
    selected_rows <- input$table_rows_selected - 1
    selected_rows <- t(matrix(selected_rows))

    plotly::plotlyProxyInvoke(
      plotly::plotlyProxy("graph", session),
      "restyle",
      list(selectedpoints = selected_rows), # arg needs a row-major matrix
      0  # this number represents the plot trace. No arg updates all traces
    )
  })
}


shinyApp(ui, server)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行可复现的 Shiny 示例,并跟踪 plotly::event_data 和 plotly::plotlyProxyInvoke 调用,尤其是使用 selectedpoints 的 restyle 操作。检查 plotly.R 使用的 JavaScript 集成,因为报告指出 JavaScript 知识有限是一个障碍。当在构建图形或执行 restyling 时提供的 selectedpoints 反映在存储的选定数据中时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, r
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。