R绘图:选择框-提取x和y坐标

时间:2020-06-16 08:59:04

标签: r shiny r-plotly

对于我的项目,我需要提取“ Box Select”的x和y坐标,该坐标用于在闪亮的应用程序中选择数据(因为我需要根据时间范围内的这些值进行过滤)。更精确地说-我只需要创建的框的实际坐标,而不需要里面选定ID的x / y值。

JS - Event Handlers <-我在这里看到事件处理程序具有这些坐标(x和y数组),您可以在控制台中看到它们-但是如何在R中动态存储它们?

已经感谢。

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)

2 个答案:

答案 0 :(得分:2)

您可以使用event_data调用来提取数据:

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

答案 1 :(得分:0)

尝试了很多之后,我发现关于框范围的数据没有存储在“ selected”的event_data中,但是在“ brushed”和“ brushing”中都可用。

这是我获取已创建框的范围的解决方案:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    updateUI(isSelected: selected)
}

private func updateUI(isSelected: Bool){
   label.textColor = isSelected ? .red : .green
   //etc..
}
相关问题