firmo23
firmo23

Reputation: 8444

Add an "all" option under the filter that selects the number of rows displayed in a datatable

I have basic shiny app with a datatable as you can see below. I was wondering if is possible to add an "All" option under the filter that selects the number of rows you want to see. I guess that I could just add the total number of rows of the mpg dataset but I would like to add a label "All" instead.

#ui.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

fluidPage(
  titlePanel("Basic DataTable"),

  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
        selectInput("man",
                    "Manufacturer:",
                    c("All",
                      unique(as.character(mpg$manufacturer))))
    ),
    column(4,
        selectInput("trans",
                    "Transmission:",
                    c("All",
                      unique(as.character(mpg$trans))))
    ),
    column(4,
        selectInput("cyl",
                    "Cylinders:",
                    c("All",
                      unique(as.character(mpg$cyl))))
    )
  ),
  # Create a new row for the table.
  DT::dataTableOutput("table")
)

#server.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

function(input, output) {

  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
options = list(pageLength = 5,
                   lengthMenu = c(5, 10, 15, 20))
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data
  }))

}

Upvotes: 2

Views: 146

Answers (1)

P1storius
P1storius

Reputation: 947

This works for me: (based on this)

Changes to you code are made in the server and explained below. I added server <- and ui <- to be able to run it for me locally

#ui.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)
library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Basic DataTable"),

  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
           selectInput("man",
                       "Manufacturer:",
                       c("All",
                         unique(as.character(mpg$manufacturer))))
    ),
    column(4,
           selectInput("trans",
                       "Transmission:",
                       c("All",
                         unique(as.character(mpg$trans))))
    ),
    column(4,
           selectInput("cyl",
                       "Cylinders:",
                       c("All",
                         unique(as.character(mpg$cyl))))
    )
  ),
  # Create a new row for the table.
  DT::dataTableOutput("table")
)

#server.r
# Load the ggplot2 package which provides
# the 'mpg' dataset.

server <- function(input, output) {

  # Filter data based on selections
  output$table <- DT::renderDataTable({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data <- DT::datatable(data=data, options = list(pageLength = 5,lengthMenu = list(c(5,10,15,20, -1), list('5', '10', '15','20', 'All')), paging = T))
  })

}

shinyApp(ui = ui, server = server)

The first thing to note is to assign the DT::datatable to your data variable.
That, combined with using -1 to select all rows and lengthMenu() as a vector of values and list of names, does the trick.

Upvotes: 2

Related Questions