I am using R Markdown to create a PDF document with a few tables.
Here is the example necessary data
output:
pdf_document:
toc: yes
require("pacman")
p_load(tidyverse, reshape, reshape2, knitr, kableExtra, tinytex, scales, pander, janitor, gridExtra)
segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3')
subSegment<- c('subseg1', 'subseg2', 'subseg1', 'subseg2', 'subseg1', 'subseg2')
var.1<- c(100, 20, 30, 50, 40, 40)
var.2<- c(200, 30, 30, 70, 30, 140)
var.3<- c(50, 50, 40, 20, 30, 40)
var.4<- c(60, 50, 35, 53, 42, 20)
df<- data.frame(segment, subSegment, var.1, var.2, var.3, var.4)
df%>%adorn_totals('row')
df.2<-df
df.2[c(3:ncol(df.2))] = sapply(df.2[c(3:ncol(df.2))], function(x) scales::percent(x, accuracy = 0.1))
df.2 %>%
kable(format = "latex", booktabs = TRUE, caption = "Title" ,align = "c") %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header", "scale_down"), font_size = 6) %>%
group_rows(index = table(fct_inorder(df$`segment`)), italic = F, bold = F, underline = T, latex_gap_space = "1em", background = "#f2f2f2")%>%
column_spec(1, monospace = T, color = "white") %>%
row_spec(nrow(df), bold = T)
I know how to use column_spec to conditionally change the format of a column, for example:
column_spec(ncol(df), color = ifelse(df$var.4 > 50, "green","red"))
I would like to give the format 'bold = T' to the cell with the maximum value in each row...
Is there a simple way to do so? this is just an example but my real df has hundreds of rows and over 20 columns so doing it manually is really not an option... I found some similar questions but mostly solving this for HTML knitting.
I trid something like:
df[3:ncol(df)] <- lapply(df[3:ncol(df)], function(x) {
cell_spec(x, font_size = spec_font_size(x))
})
but that didn't work and even if it did the results are not really what I am looking for as they change the size instead of making the maximum bold.
Short description: highlight the maximum value of each row in a data frame Edited: edited to add that numbers have a special format
Using dplyr::mutate(across... and max(c_across... is one way:
---
output:
pdf_document:
toc: yes
---
```{r, include=FALSE}
require("pacman")
p_load(dplyr, forcats, knitr, kableExtra, tinytex, janitor)
segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3')
subSegment<- c('subseg1', 'subseg2', 'subseg1', 'subseg2', 'subseg1', 'subseg2')
var.1<- c(100, 20, 30, 50, 40, 40)
var.2<- c(200, 30, 30, 70, 30, 140)
var.3<- c(50, 50, 40, 20, 30, 40)
var.4<- c(60, 50, 35, 53, 42, 20)
df <-
data.frame(segment, subSegment, var.1, var.2, var.3, var.4) %>%
adorn_totals('row') %>%
rowwise() %>%
mutate(across(var.1:var.4, ~cell_spec(.x, 'latex', bold = ifelse(.x == max(c_across(var.1:var.4)), TRUE, FALSE))))
```
```{r, results='asis'}
df %>%
kable(booktabs = TRUE,
caption = "Title",
align = "c",
escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position", "repeat_header", "scale_down"),
font_size = 6) %>%
pack_rows(index = table(fct_inorder(df$segment)),
italic = FALSE,
bold = FALSE,
underline = TRUE,
latex_gap_space = "1em",
background = "#f2f2f2")%>%
column_spec(1, monospace = TRUE, color = "white") %>%
row_spec(nrow(df), bold = TRUE)
```
Which results in this pdf output: