【问题标题】:adding/removing traces in plotly per onclick event在每个 onclick 事件中添加/删除跟踪
【发布时间】:2019-04-27 23:39:08
【问题描述】:

我正在尝试打印交互式饼图。单击绘图时,应添加另一条迹线。我为此使用 event_data。添加跟踪后,在下一次单击页面上的任意位置时,应删除跟踪。我没有找到解决方案。我不知道如何在再次单击后覆盖 onclick-event。

下一个问题是删除之前添加的跟踪。我想我可以像 Removing traces by name using plotlyProxy (or accessing output schema in reactive context) 中那样使用 plotlyProxy

之后你可以看到我的代码

library(shiny)
library(data.table)
library(plotly)

ui <- basicPage(
    mainPanel(
      fluidRow(column(8, plotly::plotlyOutput("myplot", height = "800px")))
    )
)

server <- function(input, output, session) {
  testdata = data.frame("Orga" = c("Li", "La", "Le", "Lu", "De", "Va", "Xul", "Jin"),
                    "Dachorga" = c("Bla", "Bla", "Blu", "Blu", "Blub", "Blub", "Lol", "Lol"),
                    "Umsatz.Orga" = c(20000, 10000, 12000, 3000, 100, 2400, 205000, 95000))
  testdata = data.table(testdata)
  testdata_agg = testdata[, sum(Umsatz.Orga), by=Dachorga]


  output$myplot <- renderPlotly({
    p <- testdata_agg %>%
      group_by(Dachorga) %>%
      plot_ly(labels = ~Dachorga, values = ~V1, hoverinfo = 'label+percent+value') %>%
      add_pie(hole = 0.6) %>%
      layout(title = "Donut charts using Plotly",  showlegend = F,
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
    d <- event_data("plotly_click")
    if (!is.null(d)) {
      p = add_pie(p, data = testdata[Dachorga == "Bla"], labels = ~Orga, values = ~Umsatz.Orga, hole = 0.5, 
              hoverinfo = 'label+percent+value', domain = list(
                x = c(0.1, 0.9),
                y = c(0.1, 0.9)),
              marker = list(hover = list(color = "white")))
    }
    p
  })
}

shinyApp(ui = ui, server = server)

对不起,我的英语不好,提前感谢

【问题讨论】:

    标签: r shiny r-plotly


    【解决方案1】:

    可以使用一个小的 javascript 代码来检测对文档的单击,并将结果发送到带有Shiny.setInputValue 的闪亮服务器。然后可以借助反应值来控制情节。

    library(shiny)
    library(data.table)
    library(plotly)
    
    js <- "
    $(document).ready(function(){
      $(document).on('click', function(){
        Shiny.setInputValue('click_on_doc', true, {priority: 'event'});
      })
    })"
    
    ui <- basicPage(
      tags$head(tags$script(HTML(js))),
      mainPanel(
        fluidRow(column(8, plotly::plotlyOutput("myplot", height = "800px")))
      )
    )
    
    server <- function(input, output, session) {
      testdata <- data.frame("Orga" = c("Li", "La", "Le", "Lu", "De", "Va", "Xul", "Jin"),
                            "Dachorga" = c("Bla", "Bla", "Blu", "Blu", "Blub", "Blub", "Lol", "Lol"),
                            "Umsatz.Orga" = c(20000, 10000, 12000, 3000, 100, 2400, 205000, 95000))
      testdata <- data.table(testdata)
      testdata_agg <- testdata[, sum(Umsatz.Orga), by=Dachorga]
    
      plot <- testdata_agg %>%
        group_by(Dachorga) %>%
        plot_ly(labels = ~Dachorga, values = ~V1, hoverinfo = 'label+percent+value') %>%
        add_pie(hole = 0.6) %>%
        layout(title = "Donut charts using Plotly",  showlegend = F,
               xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
               yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
    
      click <- reactiveVal(FALSE)
    
      observe({
        event <- !is.null(event_data("plotly_click"))
        click(event)
      })
    
       observeEvent(input$click_on_doc, {
         click(FALSE)
       })  
    
      output$myplot <- renderPlotly({
        if (click()) {
          p <- add_pie(plot, data = testdata[Dachorga == "Bla"], labels = ~Orga, 
                       values = ~Umsatz.Orga, hole = 0.5, 
                      hoverinfo = 'label+percent+value', domain = list(
                        x = c(0.1, 0.9),
                        y = c(0.1, 0.9)),
                      marker = list(hover = list(color = "white")))
        }else{
          p <- plot
        }
        p
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    我还没有理解你的“下一个问题”。也许打开一个新问题并尝试澄清。

    【讨论】:

    • 感谢您的帮助。您的代码已经解决了“下一个问题”。我使用来自 stackoverflow.com/questions/42996303/…> 的信息稍微修改了您的 javascript 代码:
      js  
    猜你喜欢
    • 2019-05-24
    • 2021-11-04
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多