Search code examples
rshinyflexdashboard

Using plot_click in flexdashboard


Is there a way to have plots with mouse interactions using flexdashboard?

In shiny this is not difficult. I want to save mouse clicks, and in shiny UI I would use:

mainPanel(plotOutput("scatterplot", click = "plot_click"))

And in the server you would have:

df <- reactiveValues(Clicksdf = data.frame(clickx = numeric(), clicky = numeric()))

Can I do this in flexdashboard?


Solution

  • Write the code chunk as if it were both the Shiny UI and server:

    ---
    title: "Untitled"
    runtime: shiny
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)
    ```
    
    Column {data-width=650}
    -----------------------------------------------------------------------
    
    ### Chart A
    
    ```{r}
    plotOutput("plot1", click = "wt")
    output$plot1 <- renderPlot({
      plot(mtcars$mpg ~ mtcars$wt)
      })
    ```
    
    Column {data-width=350}
    -----------------------------------------------------------------------
    
    ### Chart B
    
    ```{r}
    renderText({
      unlist(input$wt$x)
    })
    ```