Working on a shiny app (without rmarkdown), I would like to add a visual representation of a column to my data table. Inspired by this question, I produced
dat <- data.frame(
what=letters[1:15],
value=rnorm(15)
) |> transform(
direction=ifelse(abs(value)<0.1, "⇆", ifelse(value<0, "↓", "↑"))
) |> knitr::kable(format="html", escape=FALSE) |>
kableExtra::kable_styling() # show in browser
dat
which looks like this

However, I would rather use shiny::icon("circle-up") like this:
down <- as.character(shiny::icon("circle-down")) # dropping as.character results in an error
up <- as.character(shiny::icon("circle-up"))
side <- as.character(shiny::icon("circle-right"))
dat <- data.frame(
what=letters[1:15],
value=rnorm(15)
) |> transform(
direction=ifelse(abs(value)<0.1, side, ifelse(value<0, down, up))
) |> knitr::kable(format="html", escape=FALSE) |>
kableExtra::kable_styling() # show in browser
but in this case, nothing is displayed:

Any ideas how to use icons together with kable?
Assuming you're rendering to html:
Here's a reprex:

---
title: "Icons"
output: html_document
date: "2023-07-20"
---
```{r}
up <- shiny::icon(name = "circle-up") |> as.character()
down <- shiny::icon(name = "circle-down") |> as.character()
side <- shiny::icon(name = "circle-right") |> as.character()
dat <- data.frame(
what = letters[1:15],
value = rnorm(15)
) |>
transform(
direction = ifelse(abs(value) < 0.1, side, ifelse(value < 0, down, up))
) |> knitr::kable(format = "html", escape = FALSE) |>
kableExtra::kable_styling() # show in browser
dat
```
<!-- This will force shiny to load the icon deps: -->
```{r include=FALSE}
shiny::icon("circle-up")
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With