R闪闪发亮并获得图例点击事件 [英] R shiny and plotly getting legend click events
问题描述
我有一个R闪亮页面,并且正在根据单击一个饼图来过滤数据.如果可以通过单击图例条目来触发相同的过滤事件,那将是很好的选择,但是我似乎找不到该事件的触发器,因此它只是过滤该图表而不传播到其他图表.图例点击事件可以访问吗?
I have an R shiny page, and am filtering the data based on clicking a pie graph. It would be great if I could trigger the same filtering event from clicking legend entries, but I can't seem to find the event trigger, so it just filters that chart without propagating to the other charts. Is a legend click event accessible?
library(data.table)
library(plotly)
library(shiny)
dt = as.data.table(mtcars)
ui <- fluidPage(
plotlyOutput("pie1"),
plotlyOutput("pie2")
)
server <- function(input, output){
gearDT = reactive({
return(dt[,.N,by=gear])
})
cylDT = reactive({
return(dt[,.N,by=cyl])
})
output$pie1 <- renderPlotly({
plot_ly(gearDT(), labels = ~gear, values = ~N, type = "pie") %>%
layout(showlegend = TRUE)
})
output$pie2 <- renderPlotly({
plot_ly(cylDT(), labels = ~cyl, values = ~N, type = "pie") %>%
layout(showlegend = TRUE)
})
}
shinyApp(ui = ui, server = server)
推荐答案
供将来的读者使用
现在Plotly创建了一个名为plotly_relayout
的事件.布局更改时触发此事件.单击图例是这些更改之一.
Plotly now has created an event called plotly_relayout
. This event is triggered on layout changes. Clicking on the legend is one of these changes.
此事件中的一个变量称为hiddenlabels
.此变量包含所有隐藏的图例跟踪的名称.
One of the variables in this event is called hiddenlabels
. This variable contains all the names of the legend traces that are hidden.
observe({
relayout <- event_data("plotly_relayout")
hidden_labels <- relayout$hiddenlabels
print(hidden_labels)
})
编辑
Check event when clicking a name in the legend of a plotly's graph in R Shiny if plotly_relayout
is not working for you.
这篇关于R闪闪发亮并获得图例点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!