【问题标题】:I am failing to set datatable column width in R Shiny我无法在 R Shiny 中设置数据表列宽
【发布时间】:2019-12-08 14:42:08
【问题描述】:

我正在尝试在数据表中显示我的项目数据,但有些项目的描述字段中有一些非常长的字符串,因此整个数据表变得非常宽。我想以某种方式缩小整个事情,但似乎没有任何效果。我已经尝试过使用它:

div(DT::dataTableOutput("tableoutput"), style = "font_size: 75%; width: 75%")

还尝试在主面板中添加width = "XXXpx",但表格仍然太宽。我想知道是否有办法以某种方式包装长字符串或设置列宽?

这是给我带来麻烦的代码: 界面

mainPanel(
      verbatimTextOutput("textoutput"),
      div(DT::dataTableOutput("tableoutput"), style = "font_size: 75%; width: 75%")

    )

服务器

server <- function(input, output, session) {
 observeEvent(input$selectinput, {
 selected_mod <- as.numeric(str_extract(input$selectinput, "(?<=\\]\\[)(\\d+)"))
    temp <- rvalues$fields[rvalues$fields$mod_id == selected_mod,]
    temp$mod_id = NULL
    temp <- temp[, basic_score:=as.numeric(basic_score)]
    temp$condition <- ifelse(as.numeric(temp$basic_score != 0), 1, 0)
    output$tableoutput <- DT::renderDataTable(
      DT::datatable(temp, options = list(paging = FALSE,
                                         searching = FALSE,
                                         columnDefs = list(
                                           list(targets = 6,visible = FALSE)
                                           )))
      %>% formatStyle(
        'basic_score', 'condition',
        backgroundColor = styleEqual(c(0, 1), c('red', 'green'))
      )
})
}

【问题讨论】:

  • 你看过this post吗?它似乎相关,并且可能会有所帮助

标签: r shiny dt


【解决方案1】:

您可以在option= 中包含一些参数来设置宽度并进行其他更改。

例如,以下代码不包括第 6 列,并将 mtcars 的第 1 列和第 3 列的宽度设置为 50 像素

     DT::datatable(mtcars, options = list(
      autoWidth = TRUE,
      columnDefs = list(list(targets = 6,visible = FALSE),
         list(width = '50px', targets = c(1, 3)))
    ))

查看https://rstudio.github.io/DT/options.html了解更多详情

【讨论】:

  • 是的,但我遇到了麻烦,因为我不确定如何组合许多不同的选项,例如我的代码有一个选项可以使一列不可见...columnDefs = list( list(targets = 6,visible = FALSE) )
  • columnDef 里面应该有几个列表: columnDefs = list( list(targets = 6,visible = FALSE), list(width = '50px', targets = c(1, 3)) )
猜你喜欢
  • 2019-08-08
  • 1970-01-01
  • 2017-07-28
  • 2021-01-10
  • 2017-09-01
  • 2019-03-25
  • 2014-06-16
  • 2013-11-18
  • 2022-01-21
相关资源
最近更新 更多