Search code examples
rr-markdownknitrfont-awesomebookdown

How can I add a fontawesome icon to a table in Rmarkdown?


I'm looking for tidy way to add a hyperlink incorporating a fontawesome icon to an Rmarkdown table (kable) — for incorporation in an html bookdown page.

In other parts of my document, I've used the icon package, to render a hyperlinked fontawesome icon (outside of a table) using standard markdown syntax, e.g.:

`r icon::fa("file-pdf", size = 5)](https://www.google.com/){target="_blank"}`

enter image description here

But this approach doesn't work when I've attempted to incorporate it as part of a kable.

```{r}

library(icon)
library(knitr)
library(tidyverse)

## note this code throws the following error: Error in 
## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = 
## stringsAsFactors) : cannot coerce class "c("knit_asis", 
## "knit_icon")" to a data.frame

link_location <- "www.google.com"

data_test_1 <- data.frame(
  file = c('Version 1', 'Version 2', 'Version 3'),
  last_updated = Sys.Date(),
  pdf_logo = icon::fa("file-pdf")) %>%
  mutate(pdf_logo = cell_spec(pdf_logo,
    link = link_location)) %>%
  kable("html", escape = F, align = "c")
data_test_1
```

So far I've come up with a workaround that involves downloading the .svg file from the fontawesome website and adding it as an image. It works... sort of, but I would prefer to have the ability to change the size of the icon and make it more easily reproducible.

This is the code for my current workaround.

```{r fontawesome_table ='asis'}

library(tidyverse)
library(kableExtra)

## download svg from location manually
https://fontawesome.com/icons/r-project?style=brands

data_test_2 <- data.frame(
  file = c('Version 1', 'Version 2', 'Version 3'),
  last_updated = Sys.Date(),
  R_logo = "![](r-project-brands.svg)") %>%
  mutate(R_logo = cell_spec(R_logo, link = "https://cran.r- 
  project.org/")) %>%
  kable("html", escape = F, align = "c")
data_test_2
```

Which produces this output...

Output produced by chunk above

Does anyone have any ideas for how I could either, adjust the size the icon in the table, or call the icon from another package/css to create a more tidy solution?


Solution

  • Here is a way using the fontawesome package instead. I also had to use a custom link building function:

    ```{r, echo = F, message=F, warning=F}
    library(fontawesome)
    library(knitr)
    library(tidyverse)
    library(kableExtra)
    ## note this code throws the following error: Error in 
    ## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = 
    ## stringsAsFactors) : cannot coerce class "c("knit_asis", 
    ## "knit_icon")" to a data.frame
    
    link_location <- "www.google.com"
    
    addLink <- function() {
      paste0("<a href=\"", link_location, "\">", as.character(fa("file-pdf")), "</a>")
    }
    
    data_test_1 <- data.frame(file = c('Version 1', 'Version 2', 'Version 3'),
                              last_updated = Sys.Date(),
                              pdf_logo = addLink())
    
    kable(data_test_1, escape = F, align = "c")
    ```
    

    enter image description here