简体   繁体   English

R Shiny DT 数据表列宽适用于所有列,但不适用于特定列

[英]R Shiny DT data table column width works on ALL columns, but not on specific column

I have looked at the documentation, and examples, and other answers.我查看了文档、示例和其他答案。 But, for the life of me, I can't get the DT::datatable() to widen just one column in my output.但是,就我的一生而言,我无法让DT::datatable()仅扩大 output 中的一列。 When I set the option to include _all columns, it works, but obviously not what I want.当我将选项设置为包含_all列时,它可以工作,但显然不是我想要的。

Here is a working example:这是一个工作示例:

library(dplyr)
library(shiny)
library(DT)
library(data.table)

mtcars <- mtcars[1:5, ]

ui <- fluidPage(
  fluidRow(
    dataTableOutput(('mtcarsDT')),
    )
  )

server <- function(input, output, session) {
  output$mtcarsDT <- DT::renderDataTable({
    recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
      as.character(
        radioButtons(
          paste0(
            'rec', recnum),
          '',
          choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
          inline = TRUE
        )
      )
    )
    recFeedbackCol <- tibble(feedback = recFeedbackCol)
    
    mtcars <- bind_cols(
      mtcars,
      recFeedbackCol
      )
    
    mtcars %>%
      DT::datatable(
        escape = FALSE, selection = 'none',
        extensions = 'FixedColumns',
        options = list(
          paging = FALSE, pageLength = 10, ordering = FALSE,  scrollX = TRUE,
          fixedColumns = list(leftColumns = 2),
          preDrawCallback = JS(
            'function() { Shiny.unbindAll(this.api().table().node()); }'
            ),
          drawCallback = JS(
            'function() { Shiny.bindAll(this.api().table().node()); } '
            ),
          autoWidth = TRUE,
          columnDefs = list(list(width = '200px', targets = 'feedback'))
          )
        )
    })
  }

shinyApp(ui = ui, server = server)

Changing targets = '_all' works.更改targets = '_all'有效。 But, that widens all columns.但是,这会扩大所有列。

> packageVersion('shiny')
[1] ‘1.4.0’
> packageVersion('DT')
[1] ‘0.17’

Anything I am missing?有什么我想念的吗?

Update:更新:

Now, I am using ncol(mtcars) and with some options, and the DT does not render at all.现在,我正在使用ncol(mtcars)并带有一些选项,而 DT 根本不渲染。 I get the columns, and zero rows in display:我得到了显示的列和零行:

library(dplyr)
library(shiny)
library(DT)
library(data.table)

mtcars <- mtcars[1:5, ]

ui <- fluidPage(
  fluidRow(
    dataTableOutput(('mtcarsDT')),
    )
  )

server <- function(input, output, session) {
  output$mtcarsDT <- DT::renderDataTable({
    recFeedbackCol <- lapply(1:nrow(mtcars), function(recnum)
      as.character(
        radioButtons(
          paste0(
            'rec', recnum),
          '',
          choices = c('good' = 'Good', 'bad' = 'Bad', 'neutral' = 'Neutral'),
          inline = TRUE
        )
      )
    )
    recFeedbackCol <- tibble(feedback = recFeedbackCol)
    
    mtcars <- bind_cols(
      mtcars,
      recFeedbackCol
      )
    
    mtcars %>%
      DT::datatable(
        extensions = 'FixedColumns',
        rownames = FALSE,
        escape = FALSE,
        class="compact cell-border",
        options = list(
          pageLength = 15, 
          lengthChange = FALSE, 
          scrollX = TRUE,
          searching = FALSE,
          dom = 't',
          ordering = TRUE,
          fixedColumns = list(leftColumns = 2),
          preDrawCallback = JS(
            'function() { Shiny.unbindAll(this.api().table().node()); }'
          ),
          drawCallback = JS(
            'function() { Shiny.bindAll(this.api().table().node()); } '
          ),
          autoWidth = TRUE,
          columnDefs = list(
            list(width = '200px', targets = ncol(mtcars))
          )
        )
      )
    })
  }

shinyApp(ui = ui, server = server)

For targets you can use a column index number (indexing is zero-based):对于targets ,您可以使用列索引号(索引从零开始):

targets = 3

Or to target multiple specific columns, use an array:或者要定位多个特定列,请使用数组:

targets = list(3,4)

There are some additional options, too - see here for reference.还有一些其他选项 - 请参阅此处以供参考。


Update更新

So, using the fact mentioned above about indexing being zero-based, and looking at the reference documentation (see the above link), we can say the following:因此,使用上面提到的关于索引从零开始的事实,并查看参考文档(参见上面的链接),我们可以说以下内容:

  • The first column in the table has an index of 0.表中第一列的索引为 0。
  • The second column in the table has an index of 1.表中第二列的索引为 1。
  • ... and so on. ... 等等。

And we can also say:我们也可以说:

  • The last column in the table has an index of -1.表中最后一列的索引为 -1。
  • The second-to-last column in the table has an index of -2.表中倒数第二列的索引为 -2。
  • ... and so on. ... 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM