【问题标题】:R-Plotly: Box Select - Extract x & y CoordinatesR-Plotly:框选择 - 提取 x 和 y 坐标
【发布时间】:2020-06-16 08:59:04
【问题描述】:

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

JS - Event Handlers

已经谢谢了。

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)

【问题讨论】:

    标签: r shiny r-plotly


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 嘿 - 感谢您的快速回答。我意识到我的问题需要更清楚。我需要选择框的 X 和 Y 坐标而不是所选样本的坐标。一种解决方法是取 x/y 的最小值和最大值,但这只会“代表”盒子,而不是得到盒子的实际坐标。
    • 你的意思是像素还是数值?
    • 绘图的实际 x/y 值,因为我需要根据它过滤我的真实数据(大约是 100 万个数据点)。由于这个大型数据集的绘图性能(即使使用 toWebGl 和部分捆绑包)非常糟糕,我必须对其进行子集化,过滤这些,然后在整个数据集上使用相同的选择框。
    • 输出表中的curveNumber和plotNumber列是什么?
    【解决方案2】:

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

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

    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, 
                type = 'scatter') %>%
          layout(dragmode = "select") %>%
          event_register(event = "plotly_brushed")
      })
    
      # Drag based selection in plotly graph
      selected_range <- reactiveVal({})
      observeEvent(event_data("plotly_brushed"), {
        # storing the values in a reactive value for later use
        selected_range(event_data("plotly_brushed"))
    
        # alternative method if you want to use it within the same observer/reactive  expression
        #xmin <- event_data("plotly_brushed")$x[1]
        #xmax <- event_data("plotly_brushed")$x[2]
        #ymin <- event_data("plotly_brushed")$y[1]
        #ymax <- event_data("plotly_brushed")$y[2]
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多