14

I'm trying to produce an HTML table output from an R data frame and am unable to get some long column names to wrap for a multi-line header row. Here is a minimal reproducible example of my current code and output:

library(datasets)
library(knitr)
library(kableExtra)

data(iris)

Output top 5 rows of iris table as html formatted table:

sink('my_file_path.html')
names_spaced <- c('Sepal Length', 'Sepal Width', 'Petal Length', 
                  'Petal Width Long Col Name', 'Species')

kable(head(iris), 
      format='html', 
      digits=1, 
      row.names=FALSE, 
      align='lccccc', 
      col.names = names_spaced)

sink()

When I open the saved file in the browser, my header row is just one line, but I need the words to wrap to one or two lines (hence 'Petal Width Long Col Name').

kable_styling function has a param bootstrap_options but that doesn't seem to have what I need. I also tried inserting \n within the column-names but to no avail.

I am not averse to using the xtable package instead of kable/knitr if that's part of the solution.

halfer
  • 19,824
  • 17
  • 99
  • 186
Max Power
  • 8,265
  • 13
  • 50
  • 91
  • Please load all libraries that are required to run your code in the question. I guess you meant to use `kable` from the `knitr` library? – Gregor de Cillia Jun 29 '17 at 04:29

1 Answers1

14

It is possible to create linebreaks with HTML syntax. In order for that to work, you will have to set the escape argument of kable to FALSE.

library(knitr)
data(iris)

sink('my_file_path.html')
names_spaced <- c(
  'Sepal Length', 'Sepal Width', 'Petal Length', 
  'Petal Width<br/> Long Col Name',                  ## add <br/>
  'Species')

kable(head(iris), 
      format='html', 
      digits=1, 
      row.names=FALSE, 
      align='lccccc', 
      col.names = names_spaced,
      escape = FALSE)                                ## disable html escape to 
                                                     ## make <br/> work

sink()
Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • thanks a lot man, works perfectly. Elegant and customizable (easy to control exactly where line breaks happen). `?kable` documentation for `escape` param says `escape special characters when producing HTML or LaTeX tables`. That's a little ambiguous to me. Can that be used to render other html tags like e.g. in a string column value? – Max Power Jun 29 '17 at 04:42
  • I think it does altough I only worked with similar frameworks in the past -- not with `kable` itself – Gregor de Cillia Jun 29 '17 at 04:45
  • Interesting, thanks. Can I ask what your preferred tool for creating HTML tables from r dataframes is? – Max Power Jun 29 '17 at 04:46
  • 1
    It is `datatable` from the package `DT`. I strongly suggest you to check it out. – Gregor de Cillia Jun 29 '17 at 04:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147882/discussion-between-gregor-de-cillia-and-max-power). – Gregor de Cillia Jun 29 '17 at 04:57
  • Hello! Thank you very much for your answer. Is there an equivalent of
    for pdf/LaTex? When I use
    , the results show perfectly when I run them in the console but they fail to print in a pdf output. Thanks so much!
    – user3047435 Sep 12 '18 at 18:15