plotly / plotly/plotly.R

`selectedpoints` does not function properly when a vector is passed using `plotlyProxy` & `"restyle"`.

Open
#2,237 1 comment 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/Unexpected Behavior

When updating a scatter plot's selectedpoints in a Shiny server using "restyle" on a plotlyProxy, single value vectors don't update the graph. E.g., passing c(1) to selectedpoints won't update the graph.

However, if we duplicate this value, e.g., c(1, 1) and pass it to selectedpoints, the 1 point will be selected.

Reproducible example:
# selected points single value incorrect behavior
library(shiny)

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

server <- function(input, output, session) {
  output$table <- DT::renderDT({
    cars
  })
  
  output$graph <- plotly::renderPlotly({
    plotly::plot_ly(
      cars,
      x = ~ speed,
      y = ~ dist,
      selected = list(marker = list(color = "red")),
      type = "scatter",
      mode = "markers",
      source = "graph"
    ) %>%
      plotly::layout(dragmode = "select",
                     clickmode = "event+select") %>%
      plotly::event_register("plotly_selected")
  })
  
  plot_proxy <- plotly::plotlyProxy("graph")
  table_proxy <- DT::dataTableProxy("table")
  
  selected <-
    reactive({
      plotly::event_data("plotly_selected", source = "graph")
    })
  
  observeEvent(selected(), {
    selected <- selected()$pointNumber + 1
    table_proxy %>% DT::selectRows(selected)
  })
  
  observeEvent(input$table_cell_clicked, {
    selected_rows <- input$table_rows_selected - 1

   #### comment this conditional to see the bug ####
    if (length(selected_rows) == 1) {
      selected_rows <- rep(selected_rows, 2)
    }
   #### end comment this conditional to see the bug ####

   plotly::plotlyProxyInvoke(
      plotly::plotlyProxy("graph", session),
      "restyle",
      list(selectedpoints = list(selected_rows)),
      0
    )
  })
  
}

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 supplied R Shiny reproducible example and compare plotlyProxyInvoke with selected_rows containing c(1) versus c(1, 1). Trace the selectedpoints update through the plotlyProxy "restyle" call; done means a single-value vector selects the point without duplication.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.