Created
July 13, 2017 19:28
-
-
Save MrFlick/92cd189f0d839a7476cddc70ac0b3560 to your computer and use it in GitHub Desktop.
Point selection with R Shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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