Created
July 8, 2014 19:55
-
-
Save corynissen/ae8bf54ad904ca54449e to your computer and use it in GitHub Desktop.
ggvis brushing to zoom
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(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())) | |
}) | |
}) |
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(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