简体   繁体   English

R shiny 和 DT 不使用具有某些 DT 选项的特定列的列宽集呈现

[英]R shiny and DT not rendering with column width set of specific column with some DT options

I have this piece of code.我有这段代码。 The DT datatable does not render at all. datatable表根本不呈现。 It shows the columns, and nothing else.它显示列,没有别的。 I posted a related question earlier, but apparently, this issue needed to be posted as a separate question.我之前发布了一个相关问题,但显然,这个问题需要作为一个单独的问题发布。 I am.我是。

Any idea of what I am missing?知道我缺少什么吗?

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)

The problem seems to be targets parameter of columnDefs .问题似乎是columnDefstargets参数。 It accepts column index starting from 0. To specify the last column, it needs to be reduced by 1.它接受从 0 开始的列索引。要指定最后一列,它需要减 1。

  columnDefs = list(
    list(width = '200px', targets = ncol(mtcars) - 1)
  )

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

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