Get "selected data" in a parallel coordinates chart using plotly to update table

Viewed 35

As couple of users before, I want to update a table conditional on the selected lines in a parallel coordinates chart produced by plotly in R. I found a similar question for Python but don't get it working in R.

Though I get the latest dynamic interaction/selection by event_data("plotly_restyle"), I have the following issues:

  • It provides only information for the axis the user has changed the latest and no information about all other axes which are returned as NULL. Though this is annoying, it can be resolved by updating e.g. a reactive value in the background observing the "event data" (not shown).
  • Unselecting the selection area by a double click, gives back a NULL which has to be used for resetting the selection.
  • In case the user modifies the order of the axis per manually dragging the axis, I get a different output and actually which lists a perfect summary of the order of all axes/variables and their user defined limits. This is actually the output I want to get in any case, so also when just a single axis is updated.

I give a minimal example to play with and to better understand my given points above (based on a official plotly example). Any input welcome!

library("shiny")
library("plotly")
library("DT")

ui <- fluidPage(
  headerPanel("Example"),
  mainPanel(
    plotlyOutput("plot"),
    verbatimTextOutput("text"),
    DTOutput("table")
  )
)

server <- function(input, output, session) {
  
  df <- reactive({read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")})
  
  output$plot <- renderPlotly({
  
    fig <- plot_ly(
      data = df(),
      source = "myplot",
      type = "parcoords",
      line = list(color = ~species_id,
      colorscale = list(c(0, "red"), c(0.5, "green"), c(1, "blue"))),
      dimensions = list(
        list(
          range = c(2,4.5),
          label = "Sepal Width", 
          values = ~sepal_width
        ),
        list(
          range = c(4,8),
          constraintrange = c(5,6),
          label = "Sepal Length", 
          values = ~sepal_length
        ),
        list(
          range = c(0,2.5),
          label = "Petal Width", 
          values = ~petal_width
        ),
        list(range = c(1,7),
        label = "Petal Length", 
        values = ~petal_length
        )
      )
    )
    
    fig
    
    fig <- event_register(fig, "plotly_restyle")
  })
  
  output$text <- renderPrint({
    event_data("plotly_restyle", source = "myplot", session = session)
  })
  
  output$table <- renderDT({
    datatable(
      df()
    )
  })
  
}

shinyApp(ui,server)
0 Answers
Related