Home > Back-end >  R Markdown - PDF Table with conditional bold format for row maximum AND percentage format
R Markdown - PDF Table with conditional bold format for row maximum AND percentage format

Time:03-18

This question is similar to my past question: table without %

My problem is that I want the numbers to be printed as percentages. I tried using the scales::percent both before and after the conditional formating but none of them work. If I try to give the percentage format after the bold I get the error:

Error in UseMethod("round_any") : no applicable method for 'round_any' applied to an object of class "character".

If I try to use it before the conditional bold then I can't find the maximum of each row since they are characters and not numbers.

aux.n<- df
aux.n[c(3:ncol(aux.n))] = sapply(aux.n[c(3:ncol(aux.n))], function(x) scales::percent(x, accuracy = 0.1))

I should add that this is just an example but actual numbers are stuff like 0.5471927 so it's really important to print "54.7%" instead of the full number.

libraries I used:

require("pacman")
p_load(tidyverse, reshape, reshape2, knitr, kableExtra, tinytex, scales, pander, janitor)

CodePudding user response:

The percentage values are converted into character with the cell_spec argument. with a bit of stringr and regex the decimal values can be converted to percentages. Note % is a reserved symbol in LaTeX so needs escaping.

---
output:
  pdf_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE)

require("pacman")
p_load(dplyr, tidyr, stringr, kableExtra, forcats, tinytex, scales, janitor)


```{r df, include=FALSE}


        segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3', 'Tot')
        subSegment<- c('subseg1.1', 'subseg1.2', 'subseg2.1', 'subseg2.2', 'subseg3.1', 'subseg3.2', "-")
        co.1<- c(0.1, 0.4, 0.3, 0.2, 0.5, 0.4, 0.4)
        co.2<- c(0.5, 0.3, 0.3, 0.2, 0.1, 0.5, 0.4)
        co.3<- c(0.2, 0.1, 0.4, 0.4, 0.1, 0.1, 0.15)
        co.4<- c(0.2, 0.2, 0.0, 0.2, 0.3, 0.0, 0.05)
        total<- c(1,1,1,1,1,1,1)
        
        df <-
          data.frame(segment, subSegment, co.1, co.2, co.3, co.4, total) %>% 
          rowwise() %>% 
          mutate(across(co.1:co.4,  ~cell_spec(.x, 'latex', bold = ifelse(.x == max(c_across(co.1:co.4)), TRUE,  FALSE)))) %>% 
          ungroup() %>% 
          pivot_longer(starts_with("co."))%>% 
          mutate(pc = percent(as.numeric(str_extract(value, "0.\\d |0")), accuracy = 0.1),
                 value = str_replace(value, "0.\\d |0", pc),
                 value = str_replace(value, "%", "\\\\%")) %>%
          select(-pc) %>% 
          pivot_wider() %>% 
          select(-total, everything(), total)
        
```    


```{r kable, 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)

```

enter image description here

  • Related