【发布时间】:2019-05-18 02:49:56
【问题描述】:
我想使用 R Plotly 从服务器端禁用 plotly 图例选择。我们看到here 可以使用以下方法在 plotly javascript 上实现这一点,
gd.on('plotly_legendclick',function() { return false; })
我们有什么方法可以在 R 中使用 event_register() 或 event_data() 实现这一点?
我找到了一个使用 CSS 禁用图例的 hacky 解决方案。但是,如果同一 output$gg 有多个不同的绘图,CSS 代码会禁用所有绘图的图例。
代表:
最终目标,点击下面的图例一定不能隐藏任何点。
library(shiny)
library(plotly)
library(tidyverse)
ui <- fluidPage(
plotlyOutput("gg"),
verbatimTextOutput("click"),
verbatimTextOutput("doubleclick")
)
server <- function(input, output, session) {
output$gg <- renderPlotly({
p <- ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
geom_point() +
facet_wrap(~vs)
ggplotly(p) %>%
event_register("plotly_legendclick") %>%
event_register("plotly_legenddoubleclick")
})
output$click <- renderPrint({
event_data("plotly_legendclick")
})
output$doubleclick <- renderPrint({
event_data("plotly_legenddoubleclick")
})
}
shinyApp(ui,server)
【问题讨论】:
标签: r ggplot2 shiny plotly r-plotly