Rstudio闪亮的renderDataTable标题多行?

时间:2014-08-06 09:48:15

标签: r datatables shiny

我在Rstudio Shiny中有一个renderDataTable表,我用一些列构建我希望标题变为多行,这样一个长标题字符串会占用少量的水平空间。 E.g:

我的长标题列在我的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")
      )
   )
)

enter image description here

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

1 个答案:

答案 0 :(得分:2)

使用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")
                   )
)
)
)

enter image description here