2

I basically have the same question asked and answered here Fixing a column in Shiny DataTable while Scrolling Right Does not Work with the main key difference that my data table extension is Buttons and I can't change it since I need the users to be able to export the data to cxv, excel, and pdf. Still, I would need to block/freeze the first two columns. Is it possible to do that? Here is my datatable:

df <- datatable(df_data, 
                  rownames= F,
                  filter = 'top',
                  #lengthChange = T,
                  extensions = "Buttons",
                  caption = paste0("Min, Median, Max - Unfiltered data."),
                  options = list(scrollX = TRUE
                                 , autoWidth = TRUE
                                 , pageLength = 5
                                 #,  columnDefs = list(list(width = '200px', targets = c(0)  ))
                                 , initComplete = JS("function(settings, json) {","$(this.api().table().header()).css({'font-size': '10px'});","}")
                                 , dom = 'Blfrtip'
                                 ,searching = FALSE 
                                 , info = FALSE
                                 #, fixedColumns = list(leftColumns = 2)
                                 ,buttons = c('copy', 'csv', 'excel', 'pdf')
                  ))

I tried to add the fixedColumns parameter without success so far. Thanks

Angelo
  • 1,594
  • 5
  • 17
  • 50

1 Answers1

5

I'm actually glad to answer my own question. Initially, I read this useful resource: https://rstudio.github.io/DT/extensions.html However, they don't emphasize there that multiple extensions can be combined! I realized this by reading: https://github.com/rstudio/DT/issues/294 This is the final version of my table that now does allow me to freeze the first N columns:

df <- datatable(df_data, 
                  rownames= F,
                  filter = 'top',
                  #lengthChange = T,
                  extensions = c("Buttons","FixedColumns"),
                  caption = paste0("Min, Median, Max - Unfiltered data."),
                  options = list(scrollX = TRUE
                                 , autoWidth = TRUE
                                 , pageLength = 5
                                 #,  columnDefs = list(list(width = '200px', targets = c(0)  ))
                                 , initComplete = JS("function(settings, json) {","$(this.api().table().header()).css({'font-size': '10px'});","}")
                                 , dom = 'Blfrtip'
                                 ,searching = FALSE 
                                 , info = FALSE
                                 , fixedColumns = list(leftColumns = 2)
                                 ,buttons = c('copy', 'csv', 'excel', 'pdf')
                  ))

I tested and it works well in my Shiny app.

Angelo
  • 1,594
  • 5
  • 17
  • 50