首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-Plotly:框选择-提取x&y坐标

R-Plotly:框选择-提取x&y坐标
EN

Stack Overflow用户
提问于 2020-06-16 16:59:04
回答 2查看 716关注 0票数 0

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

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

已经谢谢你了。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-16 22:29:34

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

下面是我用来获取创建框的范围的解决方案:

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2020-06-16 17:04:23

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

代码语言:javascript
复制
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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62404738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档