r - Plotly and Shiny:是否可以将事件数据作为列表对象获取?
问题描述
问题:使用 plotly 和 shiny,是否可以一次提取一个 curveNumber、pointNumber、x 和 y 的列表?
示例(参见代码):通过单击每个条,p
返回下方。是否可以在不首先单击绘图的情况下获取 x 值、pointNumbers 等列表?即c(4, 6, 8)的x-object,pointNumber对象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)
解决方案
不确定你想要什么。你可以得到x
and y
with plotly_build
,见下文。而且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
推荐阅读
- javascript - Firebase ver9 调整 React 中的 snapshop/sub 集合
- node.js - 我使用 Node.js 和 GridFS 上传带有图像文件的元数据中的 UserId 和 UserName,我只能在元数据只有 UserID 时检索 Image
- android - Android 11 - 始终触发 StrictMode detectLeakedClosableObjects()
- google-cloud-platform - 有没有办法更新 slurm-gcp 中节点使用的图像?
- javascript - 从菜单项而不是顶级菜单触发模式卡 - HTML、CSS、JS
- r - 使用 Tmap() 绘制百分比图
- reactjs - React 播放列表播放器 - 获取数据并存储结果问题
- powershell - Powershell 无法使用 Multipart form-data 将超过 50MB 的 zip 文件上传到 REST API
- docker - 我们如何在 Nginx Ingress Controller 上安装动态模块(官方不支持)?(地形,舵图)
- html - Bootstrap 导航栏下拉菜单不会在单击或悬停时打开