在Shiny DataTable中固定列在向右滚动时无法工作

10

我只是想在启用了ScrollX滚动时保持最左边的列固定,但就是无法让它起作用。你有什么不同的想法吗?

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)

版本信息如下:

> 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   
2个回答

15

两个问题:

  • 你需要安装 FixedColumns 扩展;

  • 第一列实际上是 leftColumns = 2,而不是 leftColumns = 1(我猜是针对行名的)。


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

我有扩展行,但列数是问题。唉! :) - Gopala
2
leftColums = 1 works if your set rownames = FALSE - rrs
1
有没有一种方法可以在使用不同的扩展名的同时实现这个目标?例如,我必须使用 Buttons 扩展名,因为我需要允许用户将数据导出到 CSV 和 Excel,但我也需要选项来冻结表格的前两列。 - Angelo

0

Angelo 问如何同时使用 fixedColumns 和 Buttons。以下方法适用于我。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接