Ylan
Ylan

Reputation: 69

rmarkdown escape html in kable

I am trying to give my kable table's cells some custom HTML classes. But the issues is that the div gets encoded like if it was the content of the cell. I remember the shiny tables have a way to make the content of the celle escape and be interpreted as is. Is there such a thing for rmarkdown/knitr ?

For instance:

make_green  function(x) {
    x <- paste('<div class="button is-success">',x,">/div>")

    return(x)
}

new_df <- iris %>% mutate(speci = make_green(Species))
kable(head(new_df))

returns something like this:

<td style="text-align:right;"> 5.1 </td>
<td style="text-align:right;"> 3.5 </td>
<td style="text-align:right;"> 1.4 </td>
<td style="text-align:right;"> 0.2 </td>
<td style="text-align:left;"> setosa </td>
<td style="text-align:left;"> &lt;div class=&quot;button is-success&quot;&gt; setosa &lt;/div&gt; </td>

but I'd like it to return something like:

<td style="text-align:right;"> 5.1 </td>
<td style="text-align:right;"> 3.5 </td>
<td style="text-align:right;"> 1.4 </td>
<td style="text-align:right;"> 0.2 </td>
<td style="text-align:left;"> setosa </td>
<td style="text-align:left;"> <div class="button is-success"> setosa 
</div</td>

Here is the difference between the when rendering

difference between the 2

Upvotes: 2

Views: 1745

Answers (1)

user2554330
user2554330

Reputation: 44867

Use escape = FALSE:

kable(head(new_df), format = "html", escape = FALSE)

Upvotes: 5

Related Questions