Quantcast
Channel: Active questions tagged kable - Stack Overflow
Viewing all articles
Browse latest Browse all 106

How to print formated (kable) tables from a list of dataframes?

$
0
0

I have a list composed of dataframes and I want to print each list element as a knitr table on my Rmarkdown file. However, I need to format the cells on each dataframe before passing them to kable().

Basically I need to format the background of the cell in each dataframe according to one condition (red if cell value is 1, white if it is zero) and them print each dataframe as a table (booktabs style).

I am using the following code, but the tables are not formatted using the booktabs option:

library(tidyverse)library(knitr)library(kableExtra)df1 <- data.frame("2016"=c(0,0,1,1), "2017"=c(1,1,1,1), "2018"=c(0,0,1,0), "2019"=c(1,0,0,0))df2 <- data.frame("2016"=c(0,1,0,1), "2017"=c(1,0,0,1), "2018"=c(1,0,1,0), "2019"=c(1,0,1,1))rownames(df1) <- c("sp1", "sp2", "sp3", "sp4")rownames(df2) <- c("sp1", "sp2", "sp3", "sp4")df.list <- list(df1,df2)map(df.list, ~.x %>% mutate(across(everything(), ~cell_spec(., "html", background = ifelse(.x==1,  "red","white"), color = ifelse(.x==1, "red","white")))) %>% kable("html",  escape = F, booktabs = TRUE))

In my chunk options I have:

{r echo=FALSE, message=FALSE, warning=FALSE, eval=TRUE, results = "asis"}

It seems the options in kable are not being evaluated...

Any hints?

Thanks in advance:

Pedro Neves


Viewing all articles
Browse latest Browse all 106

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>