plotly / plotly/plotly.R

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

Offen
#2,238 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
R
Sterne
2.7k
Forks
641
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Ausführung des reproduzierbaren Shiny-Beispiels und verfolgen Sie die Aufrufe von plotly::event_data und plotly::plotlyProxyInvoke, insbesondere die Restyle-Operation mit selectedpoints. Untersuchen Sie die von plotly.R verwendete JavaScript-Integration, da der Bericht begrenzte JavaScript-Kenntnisse als Hindernis identifiziert. Als erledigt gilt die Aufgabe, wenn selectedpoints, die bei der Erstellung des Graphen oder beim Restyling angegeben werden, in den gespeicherten ausgewählten Daten widergespiegelt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.