Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing a column in Shiny DataTable while Scrolling Right Does not Work

Tags:

r

dt

shiny

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   
like image 372
Gopala Avatar asked Feb 22 '26 17:02

Gopala


2 Answers

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)
    )
  )
like image 165
Stéphane Laurent Avatar answered Feb 25 '26 17:02

Stéphane Laurent


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)
    )
  )
like image 39
Nir Graham Avatar answered Feb 25 '26 16:02

Nir Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!