Home > Mobile >  R Shiny DT: How to hide columns or format table?
R Shiny DT: How to hide columns or format table?

Time:01-02

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)

CodePudding user response:

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)
  • Related