Skip to content

Instantly share code, notes, and snippets.

@dgrapov
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgrapov/e1712017fe6a92b48eaf to your computer and use it in GitHub Desktop.
Save dgrapov/e1712017fe6a92b48eaf to your computer and use it in GitHub Desktop.
ggvis linked brushing bug
library(shiny)
library(ggvis)
shinyApp(
ui =bootstrapPage(
actionButton("randomize", "Randomize"),
ggvisOutput("plot1"),
ggvisOutput("plot2"),
verbatimTextOutput("summary")
),
server=function(input, output, session) {
main.data<-cocaine # staring data
get_data<-reactive({
input$randomize # need this for data update
cocaine <- main.data[sample(1:nrow(main.data),20), ]
cocaine$id <- seq_len(nrow(cocaine))
return(cocaine)
})
observe({
#new data
cocaine <- get_data()
lb <- linked_brush(keys = cocaine$id, "red") # does not reset after data update?
cocaine %>%
ggvis(~weight, ~price, key := ~id) %>%
layer_points(fill := lb$fill, fill.brush := "red", opacity := 0.3) %>%
lb$input() %>%
set_options(width = 300, height = 300) %>%
bind_shiny("plot1") # Very important!
# A subset of cocaine, of only the selected points
selected <- lb$selected
cocaine_selected <- reactive({
input$randomize
cocaine[selected(), ]
})
cocaine %>%
ggvis(~potency) %>%
layer_histograms(width = 5, boundary = 0) %>%
add_data(cocaine_selected) %>%
layer_histograms(width = 5, boundary = 0, fill := "#dd3333") %>%
set_options(width = 300, height = 300) %>%
bind_shiny("plot2")
#summary of selected (Selected does not react)
output$summary<-renderPrint({
matrix(selected())
})
})
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment