rknitrkableextra

kableExtra is turning embedded html into escaped text


I am creating a html page in knitr. However, kableExtra is turning embedded html into escaped text.

Here is a MWE for the issue.

---
title: "Animals"
output: 
  html_document:
    toc: TRUE
    toc_float: TRUE
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(knitr)
require(tidyverse)
require(formattable)
``\`
```{r animal_table, echo=FALSE, results='asis'}
animal <- tibble(animal=c("dog","cat"), height=c(18,25), weight=c(10,12)) 

animal |> 
  mutate(height = color_tile("white","#DE8C59")(height)) |> 
  knitr::kable(
  col.names=c('Animal','Height','Weight'),
  escape = F
) |> 
  kableExtra::kable_styling()
``\`
Animal  Height  Weight
dog <span style="display: block; padding: 0 4px; border-radius: 4px; background-color: #ffffff">18</span>   10
cat <span style="display: block; padding: 0 4px; border-radius: 4px; background-color: #de8c59">25</span>   12

However, if you comment out the line

#  kableExtra::kable_styling()

The table appears correctly.

Thanks in advance.


Solution

  • You should always use kableExtra::kbl() rather than knitr::kable() if you want to apply kableExtra styling.

    It's also a good idea to use FALSE instead of F, because F is a variable that can have a value other than FALSE.

    In your example that would be

    ```{r animal_table, echo=FALSE, results='asis'}
    animal <- tibble(animal=c("dog","cat"), height=c(18,25), weight=c(10,12)) 
    
    animal |> 
      mutate(height = color_tile("white","#DE8C59")(height)) |> 
      kableExtra::kbl(
        col.names=c('Animal','Height','Weight'),
        escape = FALSE
      ) |> 
      kableExtra::kable_styling()
    ```
    

    screenshot