angled column header and width - r

Please see the rmd file below. I'd like to angle some column headers and have widths be respected.
In the first table without row_spec() the widths are respected but when row_spec() is added to angle a few column headers the widths no longer hold. Any idea how to angle and have the widths remain?
rmd file
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
```{r table1, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
dt <- mtcars[1:5, 1:6]
rownames(dt) = NULL
colnames(dt)= c("long col 1","long col 12","long col 13","long col 14","long col 15","long col 16")
kable(
dt,
format ="latex",
caption = "No angle",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```
```{r table2, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
kable(
dt,
format ="latex",
caption = "With Angled columns",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
row_spec(0, angle = c(rep(0,2),rep(90,2),rep(0,2) ) )%>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```

Related

Set caption for flextable objects with align, font and italic in Rmarkdown of Word output

My little example is as follows:
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
library(flextable)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
library(magrittr) ### for %>%
ft = head(mtcars[,1:5]) %>% flextable() %>%
set_caption(caption = "display a subset of columns for mtcars data") %>%
# align(part = "caption", align = "center") %>%
# font(part = "caption", fontname = "Calibri") %>%
# italic(italic = FALSE, part = "caption") %>%
add_footer_lines("also can be based on optional argument col_keys") %>%
font(part = "footer", fontname = "Consolas") %>%
bold(bold = TRUE, part = "footer")
ft
```
The Word Rmarkdown output shows the caption with left-alignment, Cambria-font and italic. I want to customize the caption part for the table with center-alignment, some-other-font and no-italic. However, the flextable package has no part for caption, with partname of the table (one of ’all’, ’body’, ’header’, ’footer’). How can I achieve the goal?
This is only available in the development version for now:
New features have been added recently:
paragraph settings
ability to define a complex paragraph with different formats for chunks
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(flextable)
library(officer)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
```
```{r}
ft = head(mtcars[,1:5]) |> flextable() |>
set_caption(
caption = as_paragraph(
as_chunk("display a subset of columns",
props = fp_text_default(font.family = "Courier", bold = FALSE)),
as_chunk(" for mtcars data",
props = fp_text_default(color = "red", font.family = "Helvetica", bold = TRUE))
),
fp_p = fp_par(text.align = "center", padding = 5)
) |>
add_footer_lines("blah blah blah") |>
font(part = "footer", fontname = "Consolas") |>
bold(bold = TRUE, part = "footer")
ft
```
to install the current dev version:
remotes::install_github("davidgohel/officer")
remotes::install_github("davidgohel/flextable")

Background color for column in kableExtra does not work

A column in a table should have a background color. Somehow with my code this doesn't work. Here a (hopefully) reproducable example:
---
title: "Untitled"
output: pdf_document
---
```{r}
library(dplyr)
data(mtcars)
temp2 <- mtcars[1:5, 1:3]
```
```{r, results='asis'}
cat(
kableExtra::kbl(
x = temp2,
format = "latex",
digits = 2,
col.names = NULL,
booktabs = TRUE,
position = "h",
toprule = NULL,
bottomrule = NULL,
linesep = ""
) %>%
kableExtra::kable_styling(full_width = TRUE) %>%
kableExtra::column_spec(column = 2, background = "green") %>%
kableExtra::row_spec(row = nrow(temp2) - 2, hline_after = TRUE) %>%
kableExtra::row_spec(row = nrow(temp2) - 1, bold = TRUE, extra_latex_after = "\\hline \\hline")
)
```
As you can see in the output the cells are marked with \cellcolor{green} but in the pdf output the column is not green. Also I can see in the output that the first row of the colored column has not this tag \cellcolor{green}, which will be most likely my next problem. Does anybody have a suggestion how I can make this code work? Thanks in advance.

Change in Rmarkdown table the word Table

Well I have a table in Rmarkdown but since I write the document in Greek I want also the Table word to be printed in Greek
```{r name, echo=FALSE,results='asis'}
kablegr %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
what I want is instead o Table to print Πίνακας in each table of my document
Table: --> Πίνακας:
OPTION 1
This a solution that keeps the numbering automatically (following this answer. Nonetheless, I had to suppress the bold face.
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output: html_document
---
```{r, include=F}
library(captioner)
tables <- captioner(prefix = "Πίνακας")
```
```{r tab1, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab1", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
```{r tab2, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab2", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output
OPTION 2
You could try this (fig_caption: no in your YAML header):
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output:
html_document:
fig_caption: no
---
```{r name, echo=FALSE,results='asis'}
library(kableExtra)
mtcars %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output

Map cell colors to data values in kableExtra to create a heatmap table

I have a table below and would like to apply ROW-level heat maps.
(1) Any idea how? Right now the heat map is the same for both values in the single row below.
(2) Is there a way to make the header for the group column NOT be angled 90 degrees? Right now all headers are angled but for the group column it be better without angle=90.
here is the rmd file.
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(kableExtra)
#d = data.frame(group= c("A","b"),cat1=c(2,50),cat2=c(100,2))
d = data.frame(group= c("A"),cat1=c(2),cat2=c(NA))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(x, "latex", bold = F, background = spec_color(x,option="C", begin=.5, end = 0.99))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)%>% row_spec( 0,angle = 90)
```
Note: Ideally it'd be good to have this done with the kableExtra functionality so they color schemes match exactley to other kableExtra tables.
I'm actually not sure how to get the desired color mapping from spec_color. As an alternative, in the code below I've generated the color mapping using the colorRamp function and a palette-generating function, which can be adjusted as desired. The code also deals with missing values by coloring them light gray.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(viridis)
library(dplyr)
library(kableExtra)
set.seed(2)
d = data_frame(group = sample(LETTERS[1:5], 10, replace=TRUE), cat1=runif(10, 0, 100), cat2=runif(10, 0, 100))
d[5,2] = NA
max.val = max(d[ , sapply(d, is.numeric)], na.rm=TRUE)
#pal.fnc = colorRamp(viridis_pal(option="C")(2))
pal.fnc = colorRamp(c("red", "yellow", "green"))
d = d %>%
mutate_if(is.numeric, function(x) {
cell_spec(round(x,1), "latex", bold = F, color=grey(.3),
background = rgb(pal.fnc(x/max.val) %>% replace(., is.na(.), 200), maxColorValue=255))
})
```
```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8}
kable(
d, format ="latex",
linesep="",
caption = "",
booktabs = T,
longtable = T,
escape = F ,
align = "c"
) %>% kable_styling(latex_options = c(
"striped",
"repeat_header"
)
)
```

rmarkdown & kable/kableextra: Printing % symbol in Table when using escape = F

I want to create a table in a pdf-document using rmarkdwon and kable.
In the header there has to be a linebreak, so to do so i need to set escape = F.
The table itself contains percent-values as strings including the % symbol (for example: "13%").
I found here https://tex.stackexchange.com/questions/34580/escape-character-in-latex that % is a special character in La(Tex) and i have to exclude it using \%.
But how can i create and store a string "\%" in R?
I've tried the following but nothing worked for me:
gsub("%", "\%", c("14%", "15%", "16%"))
Error: '\%' is an unrecognized escape in character string starting ""\%"
gsub("%", "\\%", c("14%", "15%", "16%"))
[1] "14%" "15%" "16%"
> cat("\\%")
\%
gsub("%", cat("\\%"), c("14%", "15%", "16%"))
\%
Error in gsub("%", cat("\\%"), c("14%", "15%", "16%")) :
invalid 'replacement' argument
Does anybody know the solution for this problem?
Thanks in advance
edit:
Here is some example code for my problem:
edit of the edit:
in my made up data.frame testtable i forgot to set stringsAsFactors = F.
Thats what caused the differences to my real-data results.
I've added it to the code and updated the output-pictures
---
title: "Table with % and linbebreak"
output:
# html_document: default
pdf_document:
keep_tex: yes
fig_caption: yes
lang: de
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)
library(dplyr)
library(data.table)
library(reshape2)
library(knitr)
library(markdown)
library(kableExtra)
library(psych)
library(survey)
library(ggplot2)
set.seed(123)
testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
testtable$group = c("Var1", "Var2", "Var3", "Var4")
testtable = testtable[c(5,1:4)]
colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")
```
# Table including %-symbol, escape=F not set
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table including %-symbol, escape=F
This leads to an Error, see pic below
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table without %-symbol, escape=F set
```{r}
# removing the %
testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "", f))
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
Somehow %-symbol does not get displayed.
I have no idea how it comes, since i got other table of the same style (just without a linebreak) and the %-symbol works great.
Use way 2 with gsub("%", "\\\\%", f)).
---
title: "Untitled"
author: "Stéphane Laurent"
date: "28 août 2018"
output:
pdf_document:
keep_tex: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, error = FALSE, dev=c('pdf','png'), dpi=500)
library(dplyr)
library(data.table)
library(reshape2)
library(knitr)
library(markdown)
library(kableExtra)
library(psych)
library(survey)
library(ggplot2)
set.seed(123)
testtable = data.frame(matrix(paste0(sample(1:100,16, replace = T), "%"), nrow = 4, byrow = T), stringsAsFactors = F)
testtable$group = c("Var1", "Var2", "Var3", "Var4")
testtable = testtable[c(5,1:4)]
colnames(testtable) = c("Variable","All\n(n = 300)", "Group1\n(n = 120)", "Group2\n(n = 100)", "Group3\n(n = 80)")
```
# Table including %-symbol, escape=F not set
```{r}
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
# Table including %-symbol, escape=F
This leads to an Error, see pic below
```{r}
testtable[,2:5] = apply(testtable[,2:5],2,function(f) gsub("%", "\\\\%", f))
testtable %>% mutate_all(linebreak) %>%
kable(format = "latex", align = c("l", "c", "c", "c", "c"), caption = "Caption", booktabs = T, escape = F, col.names = linebreak(names(testtable), align = "c")) %>%
kable_styling(position = "center", latex_options = c("hold_position")) %>%
footnote(general = "* This is a note to show what * shows in this table plus some addidtional words to make this string a bit longer. Still a bit more", threeparttable = T, general_title = "Anmerkung:", title_format = "italic")
```
Isnt'it what you want? Otherwise I don't understand.
Another solution is to include the percent sign (or any other string of characters including characters you do want to escape) through custom LaTex commands defined before running kable.
```{r}
library(kableExtra)
library(tidyverse)
```
```{r}
test <- tibble(perc = c('4\\Perc', '34.2\\Perc'),
bla = c('bla','blabla'),
otherLatexCommand = c('\\RA', '\\RA'))
```
\newcommand\Perc{\%}
\newcommand\RA{$\Rightarrow$}
```{r}
test %>% kable('latex', escape=F)
```

Resources