【问题标题】:How can I add a fontawesome icon to a table in Rmarkdown?如何在 Rmarkdown 的表格中添加 fontawesome 图标?
【发布时间】:2018-11-30 00:42:11
【问题描述】:

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

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

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

产生这个输出...

是否有人对我如何调整表格中的图标大小或从另一个包/css 调用图标以创建更整洁的解决方案有任何想法?

【问题讨论】:

    标签: r r-markdown knitr font-awesome bookdown


    【解决方案1】:

    这是一种使用 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")
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2014-08-17
      相关资源
      最近更新 更多