Skip to content

Instantly share code, notes, and snippets.

@dgrapov dgrapov/app
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.