Rstudio shiny renderDataTable headers multi-line?

I have a table renderDataTable

in Rstudio Shiny that I am building with some columns. I want the title to go multi-line so that the long title line takes up a small amount of horizontal space. For example:.

My long header column is called a_very_long_header

in my data.frame and with the trick colnames

below I can turn it into a-very-long-header

, which then turns into an ugly multi-line 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

Is there a more elegant way to turn a_very_long_header

to a very long header

so that it prints a multiline header?

+3


source to share


1 answer


Use 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

+2


source







All Articles