Skip to content

Instantly share code, notes, and snippets.

@corynissen
Created July 8, 2014 19:55
Show Gist options
  • Save corynissen/ae8bf54ad904ca54449e to your computer and use it in GitHub Desktop.
Save corynissen/ae8bf54ad904ca54449e to your computer and use it in GitHub Desktop.
ggvis brushing to zoom
library(ggvis)
shinyServer(function(input, output, session) {
mtcars <- cbind(mtcars, id = seq_len(nrow(mtcars)))
# Create a linked brush object
lb <- linked_brush(keys = mtcars$id, "red")
# Just the brushed points
selected <- lb$selected
mtcars_selected <- reactive({
if (!any(selected())) return(mtcars)
mtcars[selected(), ]
})
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points(fill := lb$fill, fill.brush := "red") %>%
lb$input() %>%
add_data(mtcars_selected) %>%
layer_model_predictions(model = "lm")%>%
bind_shiny("plot1")
mtcars_selected %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
#lb$input() %>%
#add_data(mtcars_selected) %>%
#layer_model_predictions(model = "lm")%>%
bind_shiny("plot2")
output$brush_data <- renderPrint({
cat("Number of points selected: ", nrow(mtcars_selected()), "\n\n")
print(summary(mtcars_selected()))
})
})
library(ggvis)
shinyUI(bootstrapPage(
ggvisOutput("plot1"),
ggvisOutput("plot2"),
h3("Summary of brushed data (sent from client to server)"),
verbatimTextOutput("brush_data")
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment