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.
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)