1

I'm trying to format my datatable output. I want to make some changes to the table format (e.g, hide the row names) and hide columns (e.g., hide gears and carb, which I use to filter the datatable). I've read through this response before, but can't seem to get it to work. Does anyone have any suggestions for me?

I've prepared reproducible code below. In a nutshell, I'm using the mtcars dataset (my actual dataset is longer). Users can set filters, and the table output will update accordingly. It's this part of the code (under server) that isn't working:

class = "display nowrap compact"
    #filter = "top" # location of column filters
    filter = list(position = "top")
    rownames = TRUE
    options = list(dom = 't',
                   scrollX = TRUE # allow user to scroll wide tables horizontally
    )

Full code here:

library(tidyverse)
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(shinycssloaders)
library(shinythemes)
library(ggforce)
library(DT)
library(shinyWidgets)
library(shinyjs)

mtcars

ui <- fluidPage(
             sidebarLayout(
               sidebarPanel(
                 useShinyjs(),
                 div(
                   id = "form",
                   fluidRow(
                     #Button to select gear
                     column(6,
                            pickerInput(
                              inputId = "gear_button", label = "Gear:", choices = c("All", unique(as.character(mtcars$gear))), options = list(`actions-box` = TRUE), multiple = FALSE
                            ),
                     ),
                     #Button to select carb ranges
                     column(6,
                            pickerInput(inputId = "carb_button", label = "Carb:", choices = c("All", unique(as.character(mtcars$carb))), options = list(`actions-box` = TRUE), multiple = FALSE
                            ),
                     ),
                   )),
                 actionButton("resetAll", "Reset Filters")
               ),
               
               mainPanel(
                 DT::dataTableOutput("table")
               )
               
             ),
)


server <- function(input, output, session) {
  #Explore tab - table
  data <- mtcars
  output$table <- DT::renderDataTable(DT::datatable({
    data
    class = "display nowrap compact"
    #filter = "top" # location of column filters
    filter = list(position = "top")
    rownames = TRUE
    options = list(dom = 't',
                   scrollX = TRUE # allow user to scroll wide tables horizontally
    )
    
    if (input$gear_button != "All") {
      data <- data[data$gear == input$gear_button,]
    }
    if (input$carb_button != "All") {
      data <- data[data$carb == input$carb_button,]
    }
    data
  }))
  
  observeEvent(input$resetAll, {
    reset("form")
  })
  
}

shinyApp(ui, server)
Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    I think this [Q&A](https://stackoverflow.com/questions/31486738/how-do-i-suppress-row-names-when-using-dtrenderdatatable-in-r-shiny) has all the answers you're looking for. – Kat Jan 01 '22 at 18:53
  • Thanks @Kat! The other answer helped solve my issue, but I appreciate the link - helped me make some of the edits I wanted. – expanse_help Jan 01 '22 at 22:02

1 Answers1

0

We can use

options= list(columnDefs = list(list(visible = FALSE, targets = target)))

to control which columns are visible, and

target <- which(names(mtcars) %in% c("gear", "carb")) - 1

to get the position of the cols. The - 1 is because js uses 0 index instead of 1 like R.

App:

library(tidyverse)
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(shinycssloaders)
library(shinythemes)
library(ggforce)
library(DT)
library(shinyWidgets)
library(shinyjs)

mtcars

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      useShinyjs(),
      div(
        id = "form",
        fluidRow(
          # Button to select gear
          column(
            6,
            pickerInput(
              inputId = "gear_button", label = "Gear:", choices = c("All", unique(as.character(mtcars$gear))), options = list(`actions-box` = TRUE), multiple = FALSE
            ),
          ),
          # Button to select carb ranges
          column(
            6,
            pickerInput(inputId = "carb_button", label = "Carb:", choices = c("All", unique(as.character(mtcars$carb))), options = list(`actions-box` = TRUE), multiple = FALSE),
          ),
        )
      ),
      actionButton("resetAll", "Reset Filters")
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  ),
)


server <- function(input, output, session) {
  # Explore tab - table
  data <- mtcars

  table <- reactive({
    if (input$gear_button != "All") {
      data <- data[data$gear == input$gear_button, ]
    }
    if (input$carb_button != "All") {
      data <- data[data$carb == input$carb_button, ]
    }
    data
  })

  output$table <- DT::renderDataTable({
    target <- which(names(table()) %in% c("gear", "carb")) - 1

    datatable(table(),
      class = "display nowrap compact",
      filter = list(position = "top"),
      rownames = FALSE,
      options = list(
        dom = "t",
        columnDefs = list(list(visible = FALSE, targets = target)),
        scrollX = TRUE
      )
    )
  })

  observeEvent(input$resetAll, {
    reset("form")
  })
}

shinyApp(ui, server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • 1
    Thanks so much! This worked perfectly @jpdugo17, and I was able to adapt it to remove multiple columns. Appreciate it a lot. – expanse_help Jan 01 '22 at 22:00