【问题标题】:R Plotly Disable Legend Click and Legend Double ClickR Plotly禁用图例单击和图例双击
【发布时间】: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


    【解决方案1】:

    这是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; })}")
    

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      相关资源
      最近更新 更多