Plotly and Shiny:是否可以将事件数据作为列表对象获取?

Plotly and Shiny: is it possible to get event data as list object?

问题:使用plotly and shiny,是否可以一次提取曲线编号、点编号、x和y的列表?

示例(参见代码): 通过单击下方的每个柱,p returns。是否可以在最初不单击绘图的情况下获取 x 值、pointNumbers 等的列表? IE。 x-object of c(4, 6, 8), pointNumber object c(0, 1, 2)

  curveNumber pointNumber x    y
1           0           2 8 15.1
  curveNumber pointNumber x        y
1           0           1 6 19.74286

示例代码

ui <- fluidPage(
    plotlyOutput("plot")
)

server <- function(input, output) {

    output$plot <- renderPlotly({
      mtcars %>%
            group_by(cyl) %>%
            summarise(m = mean(mpg)) %>%
            plot_ly(., x = ~cyl,
              y = ~m, source = "test_plot") %>%
            add_bars()
    })

    observe({
        p <- event_data("plotly_click", source = "test_plot")
        print(p)
    })

}

# Run the application
shinyApp(ui = ui, server = server)

不确定你想要什么。你可以用 plotly_build 得到 xy,见下文。而 pointNumber 只是 0, ..., length(x)-1

p <- mtcars %>%
  group_by(cyl) %>%
  summarise(m = mean(mpg)) %>%
  plot_ly(., x = ~cyl,
          y = ~m, source = "test_plot") %>%
  add_bars() 

pp <- plotly_build(p)
curves <- pp$x$data
curveNumber <- 0
dat <- curves[[curveNumber+1]]
as.vector(dat$x)
# [1] 4 6 8
as.vector(dat$y)
# [1] 26.66364 19.74286 15.10000