使用 Shiny 中的 Datatable 创建文本换行和管理列宽

Create text wrap and manage column width with Datatable in Shiny

我有一个数据 table,其中有很多文字。我想以一种易于阅读的方式组织它。我尝试了一些想法,但它们似乎没有用。我也乐于接受有关如何组织数据的任何建议 table.

这是我试图控制列宽的代码。现在宽度非常宽,我想将选定的列缩小很多。

output$newsfeed = renderDataTable({
  datatable(df, rownames = F, extensions = "Scroller", 
          options = list(deferRender = TRUE,
                         scrollY = 400,
                         scrollX = TRUE,
                         scroller = TRUE,
                         autoWidth = TRUE,
                         columnDefs = list(list(width = '50px', targets = list(1,2,3,4,5)))
                                                                       
  ))
})

感谢您的帮助。

尝试对感兴趣的列使用 width = '10%'

library(DT)
library(shiny)

dat <- data.frame(
  V1 = c("A", "B"),
  V2 = c(
    "A cool guy living in US and Canada", 
    "A cool guy living in New York"
  ),
  V3 = c(
    "A cool guy living in US and Canada", 
    "A cool guy living in New Jersey"
  ),
  V4 = c(
    "A cool guy living in US and Canada", 
    "A cool guy living in California"
  ),
  V5 = c(
    "A cool guy living in the US and else where", 
    "A cool guy living in Texas"
  ),
  stringsAsFactors = FALSE
)

ui <- fluidPage(
  DTOutput("table")
)

server <- function(input, output){
  
  output[["table"]] <- renderDT({
    datatable(dat, options = list(
      deferRender = TRUE,
      scrollY = 400,
      scrollX = TRUE,
      scroller = TRUE,
      autoWidth = TRUE,
      columnDefs = list(list(width = '10%', targets = c(2,3,4)))
    ))  # %>% formatStyle(columns = c(2,3), width='20px')
  })
}

shinyApp(ui, server)

我猜 'extensions = "Scroller"' 控制列宽发生冲突。 当我将 'extensions = "Scroller"' 添加到 YBS 的代码时,文本不会换行。