r - Shiny & ggvis select subset of data dynamically -
how can select subset of data , plot using shiny
& ggvis
? ggvis documentation states, not possible swap dataset using ggvis
input_select
. besides limitation awesome if data preprocessing & filtering have performed once. try using selectinput()
. want able choose parts or whole dataset display
library(ggvis) library(dplyr) set.seed(1233) cocaine <- cocaine[sample(1:nrow(cocaine), 500), ] shinyserver(function(input, output, session) { output$choose_dataset <- renderui({ selectinput("dataset", "select", append("give me all!", as.list(sort(unique(cocaine$state))))) }) if(input$dataset != "give me all!"){ <- filter(rawdata, cocaine$state == input$dataset) } if(input$dataset == "give me all!"){ <- cocaine } a$id <- 1:nrow(a) return(a) datfiltered %>% ggvis(~weight, ~price, key := ~id) %>% bind_shiny("plot1") # important! })
here ui
library(ggvis) shinyui(bootstrappage( uioutput("choose_dataset"), ggvisoutput("plot1") ))
your code has few problems , doesn't run... why returning main server function? , you're using 2 variables datafiltered
, rawdata
aren't defined anywhere.
here solution of you're trying
runapp(shinyapp( ui = fluidpage( uioutput("choose_dataset"), ggvisoutput("plot1") ), server = function(input, output, session) { output$choose_dataset <- renderui({ selectinput("dataset", "select", append("give me all!", as.list(sort(unique(cocaine$state))))) }) observeevent(input$dataset, { if(input$dataset == "give me all!"){ data <- cocaine } else { data <- filter(cocaine, cocaine$state == input$dataset) } data$id <- seq(nrow(data)) data %>% ggvis(~weight, ~price, key := ~id) %>% layer_points() %>% bind_shiny("plot1") }) } ))
please try post code can run or @ least make in code saying doesn't run or variables need defined etc :)
Comments
Post a Comment