【问题标题】:Column widths in renderDataTable of a shiny app without stretching没有拉伸的闪亮应用程序的 renderDataTable 中的列宽
【发布时间】:2015-02-18 13:33:55
【问题描述】:

我想获得一个不能完全延伸到整个页面并导致每列中有大量空白的 DataTable(及其所有排名、搜索和页面功能)...

...理想情况下,列宽类似于renderTable...中的“换行”样式...

我知道我可以修复相对列宽,但是,我的表格将根据所选输入的不同列数动态更新。我希望将其他列扩展到右侧的空白区域,然后在它变得比浏览器窗口宽度更宽时触发水平滚动条。

上图中表格的可重现示例...

library(shiny)
runApp(list(
  ui = navbarPage(
  title = 'Tables',
  tabPanel('dataTableOutput', dataTableOutput('ex1')),
  tabPanel('tableOutput', tableOutput('ex2'))
),
server = function(input, output) {
  output$ex1 <- renderDataTable(iris)
  output$ex2 <- renderTable(iris)
}
))

【问题讨论】:

标签: r datatables shiny


【解决方案1】:

我认为您应该在 dataTables 中使用 drawCallback。在这里,我只是稍微更改了您的示例以将 dataTable 的宽度固定为 600px。您可以在回调函数中使用可能的 java 脚本函数来做几乎任何事情。

library(shiny)
runApp(list(
  ui = navbarPage(
    title = 'Tables',
    tabPanel('dataTableOutput', dataTableOutput('ex1')),
    tabPanel('tableOutput', tableOutput('ex2'))
  ),
  server = function(input, output) {
    output$ex1 <- renderDataTable( iris, 
                                   option = list( drawCallback = I("function( settings ) {document.getElementById('ex1').style.width = '600px';}")) )
    output$ex2 <- renderTable(iris)
  }
))

【讨论】:

    【解决方案2】:

    假设您的data.framedf,然后将此代码放在服务器端reactive/renderTable 块的开头。它会将列名包装成所需的长度,从而减小表的大小。您可以随时将宽度更改为所需的宽度。

    library(stringr)
    
    colnames(df) = str_wrap(colnames(df),width = 10)
    

    【讨论】:

      猜你喜欢
      • 2016-01-02
      • 1970-01-01
      • 2018-05-02
      • 2020-09-18
      • 2021-11-21
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多