ggplot2 Brushing

# load data in 'global' chunk so it can be shared by all users of the dashboard
library(datasets)
mtcars2 <- mtcars[, c("mpg", "cyl", "wt")]
# Reactive that returns the whole dataset if there is no brush
selectedData <- reactive({
  data <- brushedPoints(mtcars2, input$plot1_brush)
  if (nrow(data) == 0)
    data <- mtcars2
  data
})

Column {data-width=650}

Miles Per Gallon vs. Weight {data-width=600}

library(ggplot2)
plotOutput("plot1", brush = brushOpts(id = "plot1_brush"))
output$plot1 <- renderPlot({
  ggplot(mtcars2, aes(wt, mpg)) + geom_point()
})

Miles Per Gallon and Cylinders

renderPlot({
  ggplot(selectedData(), aes(factor(cyl), mpg))  + geom_boxplot()
})

Column {data-width=350}

Car Details {data-width=400}

renderTable({
  selectedData()
})


Try the flexdashboard package in your browser

Any scripts or data that you put into this service are public.

flexdashboard documentation built on Aug. 12, 2023, 1:06 a.m.