plotly / plotly/plotly.R

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

Abierto
#2,238 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
R
Estrellas
2.7k
Forks
641
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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)

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza ejecutando el ejemplo reproducible de Shiny y sigue las llamadas a plotly::event_data y plotly::plotlyProxyInvoke, especialmente la operación restyle con selectedpoints. Inspecciona la integración de JavaScript utilizada por plotly.R, ya que el informe identifica los conocimientos limitados de JavaScript como un obstáculo. Se considera terminado cuando los selectedpoints proporcionados durante la construcción del gráfico o el restyling se reflejan en los datos seleccionados almacenados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, r
Área
data-visualization
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.