r - Filter Data Range based on User Input -
i have shiny app plots 2 variables in scatter plot , filters based on user input. trying user can filter data based on range specify. however, want filter applied if field filled out, , return data if field empty (null). example, if launch code have attached, want user able check specify data ranges checkbox , when put in 4.8 in min x input box, data filtered out no data points less 4.8 considered. sample code have prepared below attempt @ i'm trying commented out near middle.
#check packages use in library { library('shiny') #allows shiny app used library('ggvis') #allows interactive ploting } alldata <- iris #establish options drop down menus { specieschoices <- unique(as.character(alldata$species)) } # ui ui<-fluidpage( titlepanel("explorer"), fluidrow( column(4, wellpanel( h4("apply filters"), selectinput(inputid = "species", label="select species:", choices = sort(specieschoices), selected="setosa", multiple = true, selectize = true) )), column(8, ggvisoutput("plot1") ), column(4, wellpanel( h4("data variables"), selectinput(inputid = "x", label="select x-axis variable:", choices=as.character(names(alldata[,1:4])),selected='petal.length', multiple = false), selectinput(inputid = "y", label="select y-axis variable:", choices=as.character(names(alldata[,1:4])),selected='petal.width', multiple = false) )), column(4, wellpanel( checkboxinput(inputid = "datarange", label="specify data ranges", value = false), conditionalpanel( condition = "input.datarange == true", wellpanel( numericinput(inputid = "minxdata", label="specify x axis min", value = -9999999999, step = 0.1), numericinput(inputid = "maxxdata", label="specify x axis max", value = 9999999999, step = 0.1), numericinput(inputid = "minydata", label="specify y axis min", value = -9999999999, step = 0.1), numericinput(inputid = "maxydata", label="specify y axis max", value = 9999999999, step = 0.1) )) )) )) #server server<-function(input,output,session) { #set reactive variables ranges filtereddata <- reactive({ minx <- input$minxdata maxx <- input$maxxdata miny <- input$minydata maxy <- input$maxydata # apply filters m <- alldata %>% filter( `species` %in% input$species ###############this part need #this works hardcoded hypothetical user input of x=petal.length , y=petal.width , petal.length >= minx, petal.length <= maxx, petal.width >= miny, petal.width <= maxy #this not work # , # as.symbol(input$x) >= minx, # as.symbol(input$x) <= maxx, # as.symbol(input$y) >= miny, # as.symbol(input$y) <= maxy ##################################################### ) m <- droplevels(as.data.frame(m)) m }) vis <- reactive({ #plot data visualization customization xvar <- prop("x", as.symbol(input$x)) yvar <- prop("y", as.symbol(input$y)) p1 = filtereddata() %>% ggvis(x = xvar, y = yvar) %>% layer_points() %>% # specifies size of plot set_options(width = 800, height = 450, duration = 0) }) #actually plots data vis %>% bind_shiny("plot1") } #run shiny app display webpage shinyapp(ui=ui, server=server)
update:
i think i'm on right track this, min x , min y filters out data if changed >= 0, , other filters not if changed.
paste0("`", input$x, "`") >= minx, paste0("`", input$x, "`") <= maxx, paste0("`", input$y, "`") >= miny, paste0("`", input$y, "`") <= maxy
i have discovered answer based on unrelated post approached how processed data in different way worked me. downloading png shiny (r)
the change within filtereddata posted below.
#set reactive variables ranges filtereddata <- reactive({ minx <- input$minxdata maxx <- input$maxxdata miny <- input$minydata maxy <- input$maxydata <- which(names(alldata)==input$x) xvariable <- as.numeric(alldata[,a]) b <- which(names(alldata)==input$y) yvariable <- as.numeric(alldata[,b]) # apply filters m <- alldata %>% filter( `species` %in% input$species, xvariable >= minx, xvariable <= maxx, yvariable >= miny, yvariable <= maxy ) m <- droplevels(as.data.frame(m)) m })
Comments
Post a Comment