Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R-Plotly: Box Select - Extract x & y Coordinates

Tags:

r

shiny

plotly

For my project I need to extract the x and y coordinates of the “Box Select” which I use to select data within a shiny app (as I need to filter according to these values within a time frame). To be more precise - I need the actual coordinates only of the created box, not the x/y values of the selected IDs inside.

JS - Event Handlers <- I saw here that the event handler has these coordinates (x and y array) and you can see them in the console - but how do I store them dynamically within R?

Thanks already.

library(shiny)
library(plotly)

ui <- fluidPage(
   plotlyOutput('myPlot'),
   )

server <- function(input, output, session){
  output$myPlot = renderPlotly({
    plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species) %>%
      layout(dragmode = "select")
  })
}

shinyApp(ui, server)
like image 660
Julian Stopp Avatar asked Sep 10 '26 05:09

Julian Stopp


1 Answers

You can extract the data using the event_data call:

library(shiny)
library(plotly)

ui <- fluidPage(
    plotlyOutput('myPlot'),
    verbatimTextOutput("se")
)

server <- function(input, output, session){
    output$myPlot = renderPlotly({
        plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species) %>%
            layout(dragmode = "select")
    })

    output$se <- renderPrint({
        d <- event_data("plotly_selected")
        d
    })
}

shinyApp(ui, server)

enter image description here

like image 188
Pork Chop Avatar answered Sep 11 '26 20:09

Pork Chop