我想使用 R Plotly 从服务器端禁用绘图图例选择。我们看here https://community.plot.ly/t/disable-legend-click-functionality-hiding-traces/1345/2可以使用以下代码在plotly javascript上实现这一点,
gd.on('plotly_legendclick',function() { return false; })
有什么方法可以在 R 中使用event_register()
or event_data()
?
我找到了一个使用 CSS 来禁用图例的 hacky 解决方案。但是,如果您对同一个图有多个不同的图output$gg
,CSS 代码禁用所有绘图的图例。
Reprex:
最终目标,点击下面的图例一定不能隐藏任何点。
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)
这是一份工作htmlwidgets::onRender
:
library(plotly)
library(htmlwidgets)
x <- c(1:15)
y <- c(1:15)
w <- gl(3,5)
dat <- data.frame(x = x, y = y, w = w)
example <- ggplot(dat, aes(x = x, y = y, color = w)) + geom_line()
ggplotly(example) %>%
onRender("function(el,x){el.on('plotly_legendclick', function(){ return false; })}")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)