Filter datatable based on slider values

2,714 views
Skip to first unread message

Endri Raco

unread,
Apr 3, 2015, 10:40:43 AM4/3/15
to shiny-...@googlegroups.com
Hi,

Mydatatable named data has a column named Frequency

In ui.R I have specified:

sliderInput("value","Name quantity:", min = 0, max = 100, value = 10)

What do I have to add in server.R in order to filter data by values of Frequency column
based on slider input?

For example, if I put in slider range 50-60 I need all the rows of dataframe who correnspond to values of Frequency 50-60

Regards

ignacio martinez

unread,
Apr 3, 2015, 11:23:31 AM4/3/15
to shiny-...@googlegroups.com
Hi Endri,

do you want something like this?


Best,

Ignacio

Endri Raco

unread,
Apr 3, 2015, 11:26:03 AM4/3/15
to shiny-...@googlegroups.com
Yes Ignacio,
Can you please provide me some code for this task?
Regards

ignacio martinez

unread,
Apr 3, 2015, 11:30:58 AM4/3/15
to shiny-...@googlegroups.com
Server:

library(shiny)
library(DT)
library(dplyr)
set.seed(422015)
DF1 <- data.frame(V1=sample(x = c("A1", "A2", "B2", "B4", "C9"), size = 100, replace = T),
                  V2=runif(n = 100,min = -100, max = 100))

shinyServer(function(input, output) {
  observe({
    
    output$tbl = renderDataTable({
      DF1 %>%
        filter(V2>input$slider2[1], V2<input$slider2[2], 
               V1 %in% input$checkGroup) %>%
        group_by(V1) %>%
        summarise(mean=mean(V2)) %>%
        datatable(extensions = 'TableTools', 
                  rownames = FALSE, 
                  options = list(pageLength = 5, lengthMenu = c(5, 10, 15, 50, 100),
                                 dom = 'T<"clear">lfrtip',
                                 tableTools = list(sSwfPath = copySWF())
                  ))
    })
    
    
  })

})


ui:

library(shiny)

shinyUI(fluidPage(
  fluidRow(

    column(4,
           
           # Copy the line below to make a slider range 
           sliderInput("slider2", label = h3("Slider Range"), min = 0, 
                       max = 100, value = c(-100, 100)),
           checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
                              choices = list("A1" = "A1", "A2" = "A2", "B2" = "B2", "B4"="B4", "C9"="C9"),
                              selected = c("A1", "A2", "B2", "B4", "C9"))
    )
  ),
  
  hr(),
  
  fluidRow(
    DT::dataTableOutput('tbl')
  )
  
))

Endri Raco

unread,
Apr 3, 2015, 11:49:07 AM4/3/15
to shiny-...@googlegroups.com
Thx Ignacio,

Tried to implement your solution:

I put in server.R:

 output$tbl = renderDataTable({
      data %>%
        filter(data$Frekuenca > input$slider2[1], data$Frekuenca<input$slider2[2]) %>%
        group_by(Viti) %>%
        datatable(extensions = 'TableTools', 
                  rownames = FALSE, 
                  options = list(pageLength = 5, lengthMenu = c(5, 10, 15, 50, 100),
                                 dom = 'T<"clear">lfrtip',
                                 tableTools = list(sSwfPath = copySWF())
                  ))
    })
and in ui.R:

 # Copy the line below to make a slider range 
    sliderInput("slider2", label = h3("Diapazoni i perseritjes"), min = 0, 
                max = 100, value = c(-100, 100))
  )

Apllication run and data show but slider doesnt change anything?

What I am doing wrong?

I removed summary and mean because I have only one column of names and one of their frequencies which needs to be filtered

ignacio martinez

unread,
Apr 3, 2015, 12:02:10 PM4/3/15
to shiny-...@googlegroups.com
take a look at my example again. You are missing the observe function.

Yihui Xie

unread,
Apr 3, 2015, 1:53:03 PM4/3/15
to Endri Raco, shiny-discuss
This is a built-in feature in the new DT package, so you do not really
have to use a separate slider. Please see the section "Column Filters"
at http://rstudio.github.io/DT/

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages