Search code examples
rr-markdownkableextra

Aesthetics of Kable Extra and Rmardown


I am reproducing the following code in R to style my table


library(kableExtra)
library(knitr)

item<-c("question1.NATURALES" ,   "question2.NATURALES"   , "question3.NATURALES"   , "question4.NATURALES",   
  "question5.NATURALES"  ,  "question6.NATURALES"  ,  "question7.NATURALES"  ,  "question8.NATURALES"   ,
  "question9.NATURALES"    ,"question10.NATURALES")

key<-c("C", "A", "A", "C", "B", "D", "B", "A", "D", "C" )
A<-c(0 ,3 ,0, 1, 0, 0 ,0 ,0, 2 ,0)
B<-c(0, 0, 1 ,0, 3, 0, 3, 1, 0, 1 )


fa<-data.frame("item"=item,"key"=key, "A"=A,"B"=B)

fa %>%
  kbl(escape = FALSE,booktabs = TRUE,longtable=TRUE) %>%
  kable_styling(fixed_thead = T,repeat_header_text = "continuaci\\'on",
        repeat_header_continued="contin\\'ua en la siguiente p\\'agina") %>%
  kable_paper(c("striped","condensed","scale_down","repeat_header"), 
              full_width = FALSE)


tmp<- data.frame(t(
  data.frame(t(select(fa, -item, -key))) %>% 
    mutate_all(function(i) cell_spec(i, color = ifelse(i == max(i), "green", "red")))
), row.names = NULL)

cbind(fa[1],fa[ 2], tmp) %>%
  kbl(escape = FALSE,booktabs = TRUE,longtable=TRUE) %>%
  kable_styling(fixed_thead = T,repeat_header_text = "continuaci\\'on",
        repeat_header_continued="contin\\'ua en la siguiente p\\'agina") %>%
  kable_paper(c("striped","condensed","scale_down","repeat_header"), 
              full_width = FALSE)

But when printing the pdf the result is as follows

enter image description here

It doesn't show stylized or show the words of the page change but if I run it in the console I get the following

enter image description here

I would like to know what is happening because in the pdf it shows me in a less aesthetic way.

Of course, if you have any other advice, it's welcome.


Solution

  • I tried to print your table using Rmarkdown. I found that we can get the striped style in PDF by using latex_options = "striped" in kable_styling() function.

    library(tidyverse)
    library(kableExtra)
    library(knitr)
    
    item<-c("question1.NATURALES" ,   "question2.NATURALES"   , "question3.NATURALES"   , "question4.NATURALES",   
      "question5.NATURALES"  ,  "question6.NATURALES"  ,  "question7.NATURALES"  ,  "question8.NATURALES"   ,
      "question9.NATURALES"    ,"question10.NATURALES")
    
    key<-c("C", "A", "A", "C", "B", "D", "B", "A", "D", "C" )
    A<-c(0 ,3 ,0, 1, 0, 0 ,0 ,0, 2 ,0)
    B<-c(0, 0, 1 ,0, 3, 0, 3, 1, 0, 1 )
    
    
    fa<-data.frame("item"=item,"key"=key, "A"=A,"B"=B)
    
    fa %>%
      kbl(escape = FALSE,booktabs = TRUE,longtable=TRUE) %>%
      kable_styling(fixed_thead = T,
            repeat_header_text = "continuaci\\'on",
            repeat_header_continued="contin\\'ua en la siguiente p\\'agina",
            latex_options = "striped") %>%
      kable_paper(c("striped","condensed","scale_down","repeat_header"), 
                  full_width = FALSE)
    
    
    tmp<- data.frame(t(
      data.frame(t(select(fa, -item, -key))) %>% 
        mutate_all(function(i) cell_spec(i, color = ifelse(i == max(i), "green", "red")))
    ), row.names = NULL)
    
    cbind(fa[1],fa[ 2], tmp) %>%
      kbl(escape = FALSE,booktabs = TRUE,longtable=TRUE) %>%
      kable_styling(fixed_thead = T,
            repeat_header_text = "continuaci\\'on",
            repeat_header_continued="contin\\'ua en la siguiente p\\'agina", 
            latex_options = "striped") %>%
      kable_paper(c("striped","condensed","scale_down","repeat_header"), 
                  full_width = FALSE)
    

    The code results in the following two tables in PDF:

    enter image description here