本文介绍了如何在R Plot';的日出图中获取点击切片的EVENT_DATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在闪亮APP中使用Plot R Package创建日出饼图时,用户可以点击该曲线图进行动态放大/缩小。我们希望能够下载当前选定/居中部分的一些数据。
但是,我们无法从所有可能的eventdata选项中找到此信息。有悬停事件,但这还不够,因为用户可以单击一块,然后在不单击它的情况下四处移动鼠标以悬停在其他块上。没有放大/缩小的单击事件。而且也没有重赛项目。我认为一定是用放大/缩小触发了一些js事件,但现有的eventdata函数没有捕获到这一点。
更新:js图表似乎有selectedPath
属性,但我不知道如何在SHINY中访问此数据。
UPDATE2:感谢您的回答,它解决了问题。此外,它还被证明是Plotly R包中缺失的功能,并且it has been addd in most recent commit。
推荐答案
目前plotly_click
仅提供日出图的根和叶的数据。但是,单击绘图后,您可以将plotly_hover
event_data
传递给reactiveVal
。
由于您尚未提供任何示例,请参阅以下内容-使用library(shinyjs)
中的onclick()
了解解决方法。
library(plotly)
library(shinyjs)
DF <- structure(list(labels = c("total", "A", "C", "B", "F", "E", "F",
"E", "D", "E", "D", "D", "F", "G", "H", "H", "G", "H", "G", "H",
"H", "G", "I", "G", "I", "H", "G", "I", "I", "G", "G", "I", "H",
"H", "I", "I", "I", "H", "I", "G"),
values = c(100L, 36L, 29L,
35L, 12L, 14L, 10L, 14L, 8L, 18L, 5L, 10L, 9L, 6L, 5L, 4L, 3L,
7L, 4L, 2L, 6L, 3L, 8L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 5L,
5L, 2L, 2L, 1L, 1L, 1L, 5L),
parents = c(NA, "total", "total", "total", "total - A", "total - C", "total - C", "total - A",
"total - B", "total - B", "total - C", "total - A", "total - B",
"total - A - F", "total - C - E", "total - C - F", "total - A - E",
"total - A - E", "total - B - D", "total - B - D", "total - B - E",
"total - C - D", "total - B - E", "total - A - D", "total - B - D",
"total - B - F", "total - C - F", "total - A - E", "total - C - E",
"total - B - E", "total - B - F", "total - A - D", "total - A - D",
"total - A - F", "total - B - F", "total - C - F", "total - C - D",
"total - C - D", "total - A - F", "total - C - E"),
ids = c(
"total", "total - A", "total - C", "total - B", "total - A - F", "total - C - E",
"total - C - F", "total - A - E", "total - B - D", "total - B - E", "total - C - D",
"total - A - D", "total - B - F", "total - A - F - G", "total - C - E - H",
"total - C - F - H", "total - A - E - G", "total - A - E - H", "total - B - D - G",
"total - B - D - H", "total - B - E - H", "total - C - D - G", "total - B - E - I",
"total - A - D - G", "total - B - D - I", "total - B - F - H", "total - C - F - G",
"total - A - E - I", "total - C - E - I", "total - B - E - G", "total - B - F - G",
"total - A - D - I", "total - A - D - H", "total - A - F - H", "total - B - F - I",
"total - C - F - I", "total - C - D - I", "total - C - D - H", "total - A - F - I",
"total - C - E - G"
)), row.names = c(NA,-40L), class = "data.frame")
ui <- fluidPage(
useShinyjs(),
plotlyOutput("sunburst"),
htmlOutput("hoverDataOut"),
htmlOutput("clickDataOut")
)
server <- function(input, output, session) {
output$sunburst <- renderPlotly({
plot_ly(data = DF, source = "sunSource", customdata = ~ids, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')
})
hoverData <- reactive({
currentEventData <- unlist(event_data(event = "plotly_hover", source = "sunSource", priority = "event"))
})
clickData <- reactiveVal()
observe({
clickData(unlist(event_data(event = "plotly_click", source = "sunSource", priority = "event")))
})
onclick(id = "sunburst", expr = {clickData(hoverData())})
output$hoverDataOut <- renderText({
paste("Hover data:", paste(names(hoverData()), unlist(hoverData()), sep = ": ", collapse = " | "))
})
output$clickDataOut <- renderText({
paste("Click data:", paste(names(clickData()), unlist(clickData()), sep = ": ", collapse = " | "))
})
}
shinyApp(ui, server)
我已创建GitHubissue以获取有关此行为的更多信息。
您可能还会对this post感兴趣。
更新:响应thisplotly_sunburstclick
的最新提交后可按如下方式使用:
# install latest r-plotly dev version:
# devtools::install_github("ropensci/plotly")
library(plotly)
library(shinyjs)
DF <- structure(list(labels = c("total", "A", "C", "B", "F", "E", "F",
"E", "D", "E", "D", "D", "F", "G", "H", "H", "G", "H", "G", "H",
"H", "G", "I", "G", "I", "H", "G", "I", "I", "G", "G", "I", "H",
"H", "I", "I", "I", "H", "I", "G"),
values = c(100L, 36L, 29L,
35L, 12L, 14L, 10L, 14L, 8L, 18L, 5L, 10L, 9L, 6L, 5L, 4L, 3L,
7L, 4L, 2L, 6L, 3L, 8L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 5L,
5L, 2L, 2L, 1L, 1L, 1L, 5L),
parents = c(NA, "total", "total", "total", "total - A", "total - C", "total - C", "total - A",
"total - B", "total - B", "total - C", "total - A", "total - B",
"total - A - F", "total - C - E", "total - C - F", "total - A - E",
"total - A - E", "total - B - D", "total - B - D", "total - B - E",
"total - C - D", "total - B - E", "total - A - D", "total - B - D",
"total - B - F", "total - C - F", "total - A - E", "total - C - E",
"total - B - E", "total - B - F", "total - A - D", "total - A - D",
"total - A - F", "total - B - F", "total - C - F", "total - C - D",
"total - C - D", "total - A - F", "total - C - E"),
ids = c(
"total", "total - A", "total - C", "total - B", "total - A - F", "total - C - E",
"total - C - F", "total - A - E", "total - B - D", "total - B - E", "total - C - D",
"total - A - D", "total - B - F", "total - A - F - G", "total - C - E - H",
"total - C - F - H", "total - A - E - G", "total - A - E - H", "total - B - D - G",
"total - B - D - H", "total - B - E - H", "total - C - D - G", "total - B - E - I",
"total - A - D - G", "total - B - D - I", "total - B - F - H", "total - C - F - G",
"total - A - E - I", "total - C - E - I", "total - B - E - G", "total - B - F - G",
"total - A - D - I", "total - A - D - H", "total - A - F - H", "total - B - F - I",
"total - C - F - I", "total - C - D - I", "total - C - D - H", "total - A - F - I",
"total - C - E - G"
)), row.names = c(NA,-40L), class = "data.frame")
ui <- fluidPage(
useShinyjs(),
plotlyOutput("sunburst"),
htmlOutput("hoverDataOut"),
htmlOutput("clickDataOut")
)
server <- function(input, output, session) {
output$sunburst <- renderPlotly({
plot_ly(data = DF, source = "sunSource", customdata = ~ids, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')
})
hoverData <- reactive({
currentEventData <- unlist(event_data(event = "plotly_hover", source = "sunSource", priority = "event"))
})
clickData <- reactive({
currentEventData <- unlist(event_data(event = "plotly_sunburstclick", source = "sunSource", priority = "event"))
})
output$hoverDataOut <- renderText({
paste("Hover data:", paste(names(hoverData()), unlist(hoverData()), sep = ": ", collapse = " | "))
})
output$clickDataOut <- renderText({
paste("Click data:", paste(names(clickData()), unlist(clickData()), sep = ": ", collapse = " | "))
})
}
shinyApp(ui, server)
这篇关于如何在R Plot';的日出图中获取点击切片的EVENT_DATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!