10

I am just trying to keep the left most column fixed when scrolling right with the ScrollX enabled, and just can't get it to work. Any idea what I need to do different?

library(shiny)
library(DT)

ui <- fluidPage(
  fluidRow(mainPanel(DT::dataTableOutput('mtcars'), width = 12))
  )
server <- server <- function(input, output, session) {
  output$mtcars <- DT::renderDataTable({
    mtcars %>%
      DT::datatable(
        selection = 'none', rownames = '', filter = 'none',
        options = list(
          paging = TRUE, searching = TRUE, info = FALSE,
          sort = TRUE, scrollX = TRUE, fixedColumns = list(leftColumns = 1)
          )
        )
    })
  }
shinyApp(ui = ui, server = server)

Version info here:

> packageVersion('DT')
[1] ‘0.4.16’
> packageVersion('shiny')
[1] ‘1.1.0’
> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          3.1                         
year           2016                        
month          06                          
day            21                          
svn rev        70800                       
language       R                           
version.string R version 3.3.1 (2016-06-21)
nickname       Bug in Your Hair   
Gopala
  • 10,363
  • 7
  • 45
  • 77

2 Answers2

14

Two problems:

  • You need the extension FixedColumns;

  • The first column is actually leftColumns = 2, not leftColumns = 1 (which is for the row names, I guess).


mtcars %>%
  DT::datatable(
    selection = 'none', rownames = '', filter = 'none',
    extensions = "FixedColumns",
    options = list(
      paging = TRUE, searching = TRUE, info = FALSE,
      sort = TRUE, scrollX = TRUE, fixedColumns = list(leftColumns = 2)
    )
  )
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • I had the extension line, but the column count is the problem. Dang! :) – Gopala Aug 01 '18 at 02:12
  • 2
    `leftColums = 1` works if your set `rownames = FALSE` – rrs May 02 '19 at 13:04
  • 1
    Is there a way to achieve this while using a different extension? For example, I have to use extension Buttons because I need to allow the users to export the data into csv and excel, but I do also need the option to freeze the first two columns of the table – Angelo Mar 24 '21 at 12:20
0

Angelo asked how to have both fixedColumns and Buttons. The below worked for me.

library(tidyverse)
library(DT)

mtcars %>%
  DT::datatable(
    selection = 'none', rownames = '', filter = 'none',
    extensions = c('Buttons','FixedColumns'),
    options = list(
        dom = 'Bfrtip',
        buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
      paging = TRUE, searching = TRUE, info = FALSE,
      sort = TRUE, scrollX = TRUE, fixedColumns = list(leftColumns = 2)
    )
  )
Nir Graham
  • 2,567
  • 2
  • 6
  • 10