3

I am trying shiny including some plotly plots. I want to use reactive events, so when I click on the plot another plot gets updated based on filtered data. The thing is that the plot I want to have the click event has facets, and I want to filter the data based on the clicked facet. But as far as I can tell, the facet info is not sent back in the event data.

I simplified the example of shiny plotly events to include facets (see the reprex below), but none of the events receive the facet info. In particular, the event_data("plotly_click") which is the one I am interested in, does not get the clicked facet anywhere.

library(plotly)
library(shiny)

ui <- fluidPage(
    plotlyOutput("plot"),
    verbatimTextOutput("hover"),
    verbatimTextOutput("click"),
    verbatimTextOutput("brush"),
    verbatimTextOutput("zoom")
)

server <- function(input, output, session) {

    output$plot <- renderPlotly({
        # use the key aesthetic/argument to help uniquely identify selected observations
        key <- row.names(mtcars)
        p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
            geom_point() +
            facet_grid(rows = "gear")
        ggplotly(p) %>% layout(dragmode = "select")
    })


    output$hover <- renderPrint({
        d <- event_data("plotly_hover")
        if (is.null(d)) "Hover events appear here (unhover to clear)" else d
    })

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        if (is.null(d)) "Click events appear here (double-click to clear)" else d
    })

    output$brush <- renderPrint({
        d <- event_data("plotly_selected")
        if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
    })

    output$zoom <- renderPrint({
        d <- event_data("plotly_relayout")
        if (is.null(d)) "Relayout (i.e., zoom) events appear here" else d
    })


}

shinyApp(ui, server)

I've been googling it and cannot find a solution. Is there anything I can pass on, for example, to the render function so it does get the facet and can send it bac in the event data?

1 Answer 1

0

well, you can see in the example code you adapted that they "use the key aesthetic/argument to help uniquely identify selected observations". Why don't you use it to identify the facet?

You would only need to tweak one line in your code, changing key = gear within aes().

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.