Link DT:datatable to a ggplot

Hi all,

Is there any way to have a ggplot update the plot based on filtered rows of a dt::datatable object? I have a shiny app that has a ggplot and a datatable both displaying data from the same dataset, but the plot's display is independent of the data currently on display in the datatable.

Thanks!

Rob

Hi, Rob

Could you post a reproducible example, called a reprex?

So here is a reprex with the solution. The quick answer I was looking for can be found here in Section 2.2 "Data Tables Information"

https://rstudio.github.io/DT/shiny.html

library(shiny)
library(DT)
library(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(
    # Application title
    titlePanel("Linking GGPlot and DT::datatable"),

    column(12,
        mainPanel(
           plotOutput("plot"),
           dataTableOutput("carsTable")
        )
    )
)

server <- function(input, output) {

    output$plot <- renderPlot({
      # here is the secret to plot only the filtered rows:
      # mtcars[input$carsTable_rows_all,]
      ggplot(mtcars[input$carsTable_rows_all,], aes(x = drat, y = mpg)) +
      geom_point()
   })
    output$carsTable <- DT::renderDataTable(mtcars, filter = "top",
                           options = list(pagelength = 100))
}

# Run the application
shinyApp(ui = ui, server = server)

Created on 2020-01-03 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.