25

My data frame has ugly column names, but when displaying the table in my report, I want to their "real" names including special characters '(', new lines, greek letters, repeated names, etc.

Is there an easy way of replacing the names in knitr to allow such formatting?

Proposed solution

What I have tried to do is suppress the printing of the data frame names and use add_header_above for better names and names that span several columns. Some advice I've seen says to use:

x <- kable(df)
gsub("<thead>.*</thead>", "", x) 

to remove the column names. That's fine, but the issue is that when I subsequently add_header_above, the original column names come back. If I use col.names=rep('',times=ncol(d.df)) in kable(...) the names are gone but the row remains, leaving a gap between my new column names and the table body. Here's a code chunk to illustrate:

```{r functions,echo=T}
drawTable <- function(d.df,caption='Given',hdr.above){
require(knitr)
require(kableExtra)
require(dplyr)

hdr.2 <- rep(c('Value','Rank'),times=ncol(d.df)/2)
x <- knitr::kable(d.df,format='latex',align='c',
  col.names=rep('',times=ncol(d.df))) %>%     
kable_styling(bootstrap_options=c('striped','hover',
  'condensed','responsive'),position='center',
   font_size = 9,full_width=F)

x %>% add_header_above(hdr.2) %>%
  add_header_above(hdr.above)
}
```

```{r}
df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
hdr.above <- c('A2','B2','C2','D2')
drawTable(df,hdr.above = hdr.above)
```

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Dr Dave
  • 453
  • 1
  • 4
  • 11
  • What output format do are you wanting your table in? HTML or PDF? You state `latex` in the `kable` command then provide HTML options to the table in the `kable_styling` – Michael Harper Jul 20 '18 at 13:31
  • I want a general solution that works for both, if that's possible. – Dr Dave Jul 20 '18 at 14:22

2 Answers2

65

I am not sure where you got the advice to replace rownames, but it seems excessively complex. It is much easier just to use the built-in col.names argument within kable. This solution works for both HTML and LaTeX outputs:

---
output:
  pdf_document: default
  html_document: default
---
```{r functions,echo=T}
require(knitr)

df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
knitr::kable(df, 
             col.names = c("Space in name",
                           "(Special Characters)",
                           "$\\delta{m}_1$",
                           "Space in name"))

```

PDF output: enter image description here

HTML output: enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • (slaps forehead) I think I went down too many web rabbit holes yesterday. Of course, this is the way. Thanks for yanking me back out! You're the best! – Dr Dave Jul 20 '18 at 16:19
  • 2
    Thank you for your helpful answer @Michael. In this case, escape = FALSE would be needed if I am not mistaken. Any suggestion on dataframe containing cells with special characters that needs the escape option to be TRUE? – Jane Mar 20 '19 at 21:03
  • Thank you. Using the special characters in the solution was very helpful. – itsMeInMiami Sep 22 '19 at 12:49
  • @Jane's comment is necessary I found in order to have the LaTeX render properly in a pdf – thus__ Dec 14 '19 at 16:02
0

If you're targeting HTML, then &Delta; is an option too.

I couldn't get the accepted answer to work on HTML, so used the above.

dsz
  • 4,542
  • 39
  • 35