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
You appear to be mixing html and LaTeX.
The booktabs
argument is for LaTeX outputs. So this may be what you are looking for:
---
output: pdf_document
---
```{r include=FALSE}
library(dplyr)
library(purrr)
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)
```
```{r results='asis'}
map(df.list, ~.x |> mutate(across(everything(), ~cell_spec(., "latex", background = ifelse(.x==1, "red","white"), color = ifelse(.x==1, "red","white")))) |>
kbl(escape = FALSE,
booktabs = TRUE))
```
With the following output:
Note: numerical variables in the call to data.frame()
are 'automatically' given "syntactically valid" names. So there may be a bit of tidying up to do or use tibble()
if you want the year as the variable name.