Rstudio闪亮的renderDataTable标题多行?

Posted

技术标签:

【中文标题】Rstudio闪亮的renderDataTable标题多行?【英文标题】:Rstudio shiny renderDataTable headers multi line? 【发布时间】:2014-09-29 04:43:37 【问题描述】:

我在 Rstudio Shiny 中有一个 renderDataTable 表,我用一些列构建了我想让标题变成多行的,这样长的标题字符串就会占用少量的水平空间。例如:

我的长标题列在我的 data.frame 中被称为 a_very_long_header,通过下面的 colnames 技巧,我可以将它变成 a-very-long-header,然后变成丑陋的多行标题:

shinyServer(function(input, output, session) 
  output$dt <- renderDataTable(
  data =     data.frame(a_very_long_header=rnorm(10),a=rnorm(10),b=rnorm(10),c=rnorm(10),d=rnorm(10),e=rnorm(10),f=rnorm(10),g=rnorm(10),h=rnorm(10),i=rnorm(10),j=rnorm(10),k=rnorm(10),a1=rnorm(10),b1=rnorm(10),c1=rnorm(10),d1=rnorm(10),e1=rnorm(10),f1=rnorm(10),g1=rnorm(10),h1=rnorm(10),i1=rnorm(10),j1=rnorm(10),k1=rnorm(10))
  colnames(data) = c("a-very-long-header","a","b","c","d","e","f","g","h","u","j","k","a1","b1","c1","d1","e1","f1","g1","h1","u1","j1","k1")
    return(data)
   )
)


shinyUI(navbarPage("Foo", id="page", collapsable=TRUE, inverse=FALSE,
   tabPanel("Bar",
       dataTableOutput("dt")
      )
   )
)

有没有更优雅的方法将a_very_long_header 转换为a very long header 以便多行打印标题?

【问题讨论】:

只是对 jdharrison 帖子的更新。您现在需要添加 escape = FALSE 以识别 DT 表中的 html。 Shiny DT Documentation 【参考方案1】:

使用HTML

library(shiny)
runApp(list(
  server = function(input, output, session) 
  output$dt <- renderDataTable(
    data =     data.frame(a_very_long_header=rnorm(10),a=rnorm(10),b=rnorm(10),c=rnorm(10),d=rnorm(10),e=rnorm(10),f=rnorm(10),g=rnorm(10),h=rnorm(10),i=rnorm(10),j=rnorm(10),k=rnorm(10),a1=rnorm(10),b1=rnorm(10),c1=rnorm(10),d1=rnorm(10),e1=rnorm(10),f1=rnorm(10),g1=rnorm(10),h1=rnorm(10),i1=rnorm(10),j1=rnorm(10),k1=rnorm(10))
    colnames(data) = c("a very<br>long header","a","b","c","d","e","f","g","h","u","j","k","a1","b1","c1","d1","e1","f1","g1","h1","u1","j1","k1")
    return(data)
  )

, ui = navbarPage("Foo", id="page", collapsable=TRUE, inverse=FALSE,
                   tabPanel("Bar",
                            dataTableOutput("dt")
                   )
)
)
)

【讨论】:

奇怪,就我而言,如果我这样做colnames(data) = chartr("-","&lt;br&gt;",colnames(data)),我只能看到标题中显示的第一个令牌...

以上是关于Rstudio闪亮的renderDataTable标题多行?的主要内容,如果未能解决你的问题,请参考以下文章

renderTable和renderDataTable之间的R闪亮不同的输出

闪亮的renderDataTable中的formatStyle

如何将renderDataTable的过滤器放在R闪亮的顶部

没有拉伸的闪亮应用程序的 renderDataTable 中的列宽

闪亮的 renderDataTable table_cell_clicked

R - 当用户在不同的页面上时,闪亮的数据表(renderDataTable)重新加载到第一页并更新某个列值(selectInput)