R数据表冻结前N列

Ang*_*elo 2 datatable r datatables shiny shinyapps

我基本上在这里提出和回答了相同的问题Fixing a column in Shiny DataTable while Scrolling Right Do not Work的主要区别是我的数据表扩展名是按钮,我无法更改它,因为我需要用户能够将数据导出到 cxv、excel 和 pdf。尽管如此,我仍然需要阻止/冻结前两列。可以这样做吗?这是我的数据表:

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')
                  ))
Run Code Online (Sandbox Code Playgroud)

到目前为止,我尝试添加fixedColumns参数但没有成功。谢谢

Ang*_*elo 5

我其实很高兴回答我自己的问题。最初,我阅读了这个有用的资源:https://rstudio.github.io/DT/extensions.html 但是,他们并没有强调可以组合多个扩展!我通过阅读意识到这一点: https: //github.com/rstudio/DT/issues/294 这是我的表的最终版本,现在允许我冻结前 N 列:

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')
                  ))
Run Code Online (Sandbox Code Playgroud)

我测试过,它在我的 Shiny 应用程序中运行良好。