如何在 Rmarkdown 中将 fontawesome 图标添加到 table?

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

我正在寻找一种简洁的方法来添加一个包含 fontawesome 图标的超链接到 Rmarkdown table (kable) — 用于合并到 html bookdown页。

在我文档的其他部分,我使用了 icon 包,使用标准降价语法呈现超链接的 fontawesome 图标(在 table 之外),例如:

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

但是当我试图将它作为 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
```

到目前为止,我想出了一个解决方法,涉及从 fontawesome 网站下载 .svg 文件并将其添加为图像。它的工作原理......有点,但我更希望能够更改图标的大小并使其更容易重现。

这是我当前解决方法的代码。

```{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
```

产生此输出...

有没有人知道我如何调整 table 中图标的大小,或者从另一个 package/css 调用图标来创建更整洁的解决方案?

这里有一种使用 fontawesome 包的方法。我还必须使用自定义 link 构建函数:

```{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")
```