Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Created July 13, 2017 19:28
Show Gist options
  • Save MrFlick/92cd189f0d839a7476cddc70ac0b3560 to your computer and use it in GitHub Desktop.
Save MrFlick/92cd189f0d839a7476cddc70ac0b3560 to your computer and use it in GitHub Desktop.
Point selection with R Shiny
library(shiny)
library(tidyverse)
mydata <- tibble::rowid_to_column(mpg)
ui <- fluidPage(
titlePanel("Sparkler"),
plotOutput("facet1", brush="facet1_brush"),
textOutput("numsel"),
actionButton("done", "Done")
)
server <- function(input, output) {
selection <- reactiveValues(
rows = numeric()
)
observeEvent(input$facet1_brush, {
selection$rows <- brushedPoints(mydata, input$facet1_brush)$rowid
})
plotdata <- reactive({
mutate(mydata, selected = rowid %in% selection$rows)
})
output$facet1 <- renderPlot({
ggplot(plotdata()) +
geom_point(aes(cty, hwy, color=selected))
})
output$numsel <- renderText({
paste(length(selection$rows), "point(s) selected")
})
observeEvent(input$done, {
stopApp(mydata[selection$rows,])
})
}
# Run the application
runApp(shinyApp(ui = ui, server = server))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment