4

Thanks to this solution I finally figured out how create dynamic SliderInput button. Unfortunately I have a problem with use this input value after all ( to change subset condition in dplyr). Could anyone tell me what I do wrong?

ui.R

library(dplyr)
library(shiny)
library(ggvis)

shinyUI(fluidPage(

titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
  radioButtons("dataset", label = h4("Product level"),
               choices = list("Item" = "df1", "Task" = "df2")),
  uiOutput("slider")
  ),
mainPanel(
  ggvisOutput("plot")
   )
 )
))

server.R

 library(shiny)
 library(dplyr)

 df1 <- data.frame(id = c(1,2,3,4,5), number = c(20,30,23,25,34))
 df2 <- data.frame(id = c(1,2), number = c(33,40))

 shinyServer(function(input, output) {

 datasetInput <- reactive({
   switch(input$dataset,
         df1 = df1,
         df2 = df2)
 })

 output$slider <- renderUI({
   sliderInput("inslider","Slider", min   = min(datasetInput()$number), 
                                   max   = max(datasetInput()$number),
                                   value = c(min(datasetInput()$number),     
                                             max(datasetInput()$number))
 })
 data <- reactive({
  datasetInput %>%
    filter(number >= input$inslider[1],
           number <= input$inslider[2])
 })

 vis <- reactive({
  data %>%
   ggvis(~id, ~number) %>%
   layer_points(fill = ~factor(id)) %>%
   scale_nominal("fill", range = c("red","blue","green","yellow","black"))

 })
 vis %>% bind_shiny("plot") 
})
Community
  • 1
  • 1
Nicolabo
  • 1,337
  • 12
  • 30

1 Answers1

2

Since you are using renderUI to make the slider, you have to check that input$inslider exists before filtering the data. When you load it for the first time, it doesn't because it is created by the renderUI

Try this for your server.R:

library(shiny)
library(dplyr)

df1 <- data.frame(id = c(1,2,3,4,5), number = c(20,30,23,25,34))
df2 <- data.frame(id = c(1,2), number = c(33,40))

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           "df1" = df1,
           "df2" = df2)
  })

  output$slider <- renderUI({
    sliderInput("inslider","Slider", min   = min(datasetInput()$number), 
                max   = max(datasetInput()$number),
                value = c(min(datasetInput()$number),     
                          max(datasetInput()$number))
  )})


  data <- reactive({
    filteredData<-datasetInput()
    if(!is.null(input$inslider)){
      filteredData<-filteredData %>%
      filter(number >= input$inslider[1] ,
             number <= input$inslider[2] )
    }
    filteredData
    })

  vis <- reactive({

    data()%>%
      ggvis(~id, ~number) %>%
      layer_points(fill = ~factor(id)) %>%
      scale_nominal("fill", range = c("red","blue","green","yellow","black"))

  })
  vis %>% bind_shiny("plot") 

})
NicE
  • 21,165
  • 3
  • 51
  • 68
  • Thank a lot! Now I understand how Shiny reacts in similar situation! – Nicolabo Feb 10 '15 at 13:11
  • One more question! I've noticed that after I change `dataframe` and range in `sliderInput` also change all dot points dissappear. Then, when I decrease range , all dot points show up. It is possible to improve it? – Nicolabo Feb 10 '15 at 15:09
  • `@NicE` Thanks once again! I have one more question related to the previous one. I describe it here http://stackoverflow.com/questions/28461220/reactive-change-min-max-range-2-dataframes-shiny I appreciate it.:) – Nicolabo Feb 11 '15 at 21:49