如何在 Rmarkdown 中的表格中添加精美的图标?

ven*_*ion 2 r knitr font-awesome r-markdown bookdown

我正在寻找一种简洁的方法来添加一个包含超棒图标的超链接到Rmarkdown表(kable)\xe2\x80\x94,以便合并到html中bookdown页面中。

\n\n

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

\n\n
`r icon::fa("file-pdf", size = 5)](https://www.google.com/){target="_blank"}`\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n\n

但是当我尝试将其合并为一个项目的一部分时,这种方法不起作用kable.

\n\n
```{r}\n\nlibrary(icon)\nlibrary(knitr)\nlibrary(tidyverse)\n\n## note this code throws the following error: Error in \n## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = \n## stringsAsFactors) : cannot coerce class "c("knit_asis", \n## "knit_icon")" to a data.frame\n\nlink_location <- "www.google.com"\n\ndata_test_1 <- data.frame(\n  file = c(\'Version 1\', \'Version 2\', \'Version 3\'),\n  last_updated = Sys.Date(),\n  pdf_logo = icon::fa("file-pdf")) %>%\n  mutate(pdf_logo = cell_spec(pdf_logo,\n    link = link_location)) %>%\n  kable("html", escape = F, align = "c")\ndata_test_1\n```\n
Run Code Online (Sandbox Code Playgroud)\n\n

到目前为止,我已经想出了一个解决方法,包括从 fontawesome 网站下载 .svg 文件并将其添加为图像。它可以工作......有点,但我更希望能够更改图标的大小并使其更容易复制。

\n\n

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

\n\n
```{r fontawesome_table =\'asis\'}\n\nlibrary(tidyverse)\nlibrary(kableExtra)\n\n## download svg from location manually\nhttps://fontawesome.com/icons/r-project?style=brands\n\ndata_test_2 <- data.frame(\n  file = c(\'Version 1\', \'Version 2\', \'Version 3\'),\n  last_updated = Sys.Date(),\n  R_logo = "![](r-project-brands.svg)") %>%\n  mutate(R_logo = cell_spec(R_logo, link = "https://cran.r- \n  project.org/")) %>%\n  kable("html", escape = F, align = "c")\ndata_test_2\n```\n
Run Code Online (Sandbox Code Playgroud)\n\n

哪个产生这个输出...

\n\n

上面的块产生的输出

\n\n

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

\n

Mar*_*zer 5

这是一种使用包的方法fontawesome。我还必须使用自定义链接构建功能:

```{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")
```
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述