1

I'm creating a app with 2 different tabs. Each of them has a couple of 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?

2
  • I don't have a proper answer to your question (regarding the support), but I have experienced the same sort of issue that you have. The solution to me was to register events from all the plots (in your case p1 and p2) and define two variables to grab the events... However, the variable for the 2nd plot was getting all the events, including for the 1st plot. Commented Jul 5, 2019 at 14:15
  • BTW, if you have multiple tabs in your app, you may be interested in using modules Commented Jul 5, 2019 at 14:23

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.