如何将多个参数传递给 gt table 格式化程序
How to pass more than one parameter to a gt table formatter
在下面的示例中,我在这里 [https://community.rstudio.com/t/create-interactive-links-in-gt-table-in-rmarkdown/70266/3][1]
格式化程序函数将 url 作为来自 um 列的参数,并使用 link 本身作为 link 的文本,我一直在尝试传递一秒钟link 的文本来自另一列的参数,例如“名称”,我该如何实现?
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(gt)
make_hyperlink = function(myurl,mytext=myurl) {
paste('<a href="',myurl,'">',mytext,'</a>')
}
df <- data.frame(
stringsAsFactors = FALSE,
country = c("UK", "US"),
name = c("BBC", "CNN"),
link = c("https://www.bbc.com/news", "https://edition.cnn.com/")
)
df %>%
gt() %>%
fmt (
columns = 'link',
fns = make_hyperlink
)
[1]: https://community.rstudio.com/t/create-interactive-links-in-gt-table-in-rmarkdown/70266/3
您可以使用 htmltools::HTML
-
library(dplyr)
library(gt)
df %>%
mutate(link = make_hyperlink(link, name)) %>%
gt() %>%
fmt (
columns = 'link',
fns = function(x) sapply(x, htmltools::HTML)
)
我们可以用 gt::html
library(dplyr)
library(purrr)
library(gt)
df %>%
mutate(link = map(make_hyperlink(link, name), html)) %>%
gt()
-输出
在下面的示例中,我在这里 [https://community.rstudio.com/t/create-interactive-links-in-gt-table-in-rmarkdown/70266/3][1]
格式化程序函数将 url 作为来自 um 列的参数,并使用 link 本身作为 link 的文本,我一直在尝试传递一秒钟link 的文本来自另一列的参数,例如“名称”,我该如何实现?
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(gt)
make_hyperlink = function(myurl,mytext=myurl) {
paste('<a href="',myurl,'">',mytext,'</a>')
}
df <- data.frame(
stringsAsFactors = FALSE,
country = c("UK", "US"),
name = c("BBC", "CNN"),
link = c("https://www.bbc.com/news", "https://edition.cnn.com/")
)
df %>%
gt() %>%
fmt (
columns = 'link',
fns = make_hyperlink
)
[1]: https://community.rstudio.com/t/create-interactive-links-in-gt-table-in-rmarkdown/70266/3
您可以使用 htmltools::HTML
-
library(dplyr)
library(gt)
df %>%
mutate(link = make_hyperlink(link, name)) %>%
gt() %>%
fmt (
columns = 'link',
fns = function(x) sapply(x, htmltools::HTML)
)
我们可以用 gt::html
library(dplyr)
library(purrr)
library(gt)
df %>%
mutate(link = map(make_hyperlink(link, name), html)) %>%
gt()
-输出