plotly / plotly/plotly.R

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

Open
#2,238 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproducible Shiny example and follow the plotly::event_data and plotly::plotlyProxyInvoke calls, especially the restyle operation with selectedpoints. Inspect the JavaScript integration used by plotly.R, since the report identifies limited JavaScript knowledge as an obstacle. Done means selectedpoints supplied during graph construction or restyling is reflected in the stored selected data.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.