对于我的项目,我需要提取“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)发布于 2020-06-16 22:29:34
在尝试了很多次之后,我发现关于盒子范围的数据并没有存储在"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)发布于 2020-06-16 17:04:23
您可以使用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)

https://stackoverflow.com/questions/62404738
复制相似问题