Click-event not triggering inside a Plotly subplot within Shiny

允我心安 提交于 2021-02-08 06:47:50

问题


I'm creating a shiny app with 2 different tabs. Each of them has a couple of plotly plots. I want the plots to only react to the plotly_click events from the correspondent tab. My problem comes when instead of having separated plots, I want one of them to be part of a subplot.

To do so, I have added the source parameter to the plots in order to distinguish which plot is the one that has been clicked. When the plots are individual plots (not inside a subplot) works nicely, but if I try to put one of them inside a compound subplot, no more events related to that plot seem to be triggered.

The working code is something like this:

ui <- navbarPage("",  
    tabPanel("Tab1",
        plotlyOutput("tab1plot1"),
        plotlyOutput("tab1plot2")
    ),
    tabPanel("Tab2",
        plotlyOutput("tab2plot1"),
        plotlyOutput("tab2plot2")
    )
)

server <- function(input, output, session) {
    output$tab1plot1 <- renderPlotly({
        plot_ly(x = dates, y = y, source = "tab1plot1", ...)
    })

    output$tab1plot2 <- renderPlotly({
        click <- event_data("plotly_click", source = "tab1plot1")
        if (!is.null(click)) {
            # process click
            plot_ly(x = dates, y = processedY, ...)
        }
        else { NULL }
    })

    output$tab2plot1 <- renderPlotly({
        plot_ly(source = "tab2plot1", ...)
    })

    output$tab2plot2 <- renderPlotly({
        click <- event_data("plotly_click", source = "tab2plot1")
        if (!is.null(click)) {
            # process click ...
        }
        else { ... }
    })

This works as expected, but the problem comes when I try to change the tab1plot1 for a compound subplot as this:

server <- function(input, output, session) {
    output$tab1plot1 <- renderPlotly({
        p1 <- plot_ly(x = dates, y = y, source = "tab1plot1", ...)
        p2 <- plot_ly( ... )
        subplot(p1, p2, nrows = 2, shareX = TRUE)
    })

   ...
}

If I do so, the plotly_click event is never triggered and so the tab1plot2 is never changed. In fact, I get a log message: "Warning: Can only have one: source Warning: The 'plotly_click' event tied a source ID of 'main' is not registered. In order to obtain this event data, please add event_register(p, 'plotly_click') to the plot (p) that you wish to obtain event data from."

Am I missing something or plotly just doesn't support the source attribute inside a subplot?


回答1:


Plotly does support source with subplot; you just have to specify the same source ID for all the plots within that subplot. One way that will work is:

output$tab1plot1 <- renderPlotly({
    p1 <- plot_ly(x = dates, y = y, ...)
    p2 <- plot_ly( ... )
    s <- subplot(p1, p2, nrows = 2, shareX = TRUE)
    s$x$source <- "tab1plot1"
    s
})

Reference: https://github.com/ropensci/plotly/issues/1504



来源:https://stackoverflow.com/questions/56901422/click-event-not-triggering-inside-a-plotly-subplot-within-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!