【发布时间】:2021-08-11 00:08:01
【问题描述】:
我希望使用 gt 库从 csv 文件创建 HTML 表格。在某些单元格中,我希望将文本和超链接结合起来。比如:
这是 BBC 的link
我找到了一个如何包含链接的示例,但不确定如何将其与文本结合。
# Src: https://community.rstudio.com/t/create-interactive-links-in-gt-table-in-rmarkdown/70266
library("tidyverse")
library("gt")
df <- tibble(
name = c("BBC", "CNN"),
link = c("https://www.bbc.com/news", "https://edition.cnn.com/")
)
# Creates a single link
df %>%
mutate(
link = map(link, ~ htmltools::a(href = .x, "website")),
link = map(link, ~ gt::html(as.character(.x)))) %>%
gt()
# Something like this would be nice
df <- tibble(
name = c("BBC", "CNN"),
link = c("Here is a [link](https://www.bbc.com/news) to the BBC", "And [here](https://edition.cnn.com/) is a link to CNN")
)
解决方案
tibble(
name = c("BBC", "CNN", "GA"),
link = c("Here is a <a href = 'https://www.bbc.com/news'>link</a> to the BBC",
"And <a href = 'https://edition.cnn.com/'>here</a> is a link to CNN",
"And here is no link")
) %>%
mutate(link = map(link, gt::html)) %>%
gt |>
需要添加,否则列中的文字会居中
cols_align( 对齐 = c("左"), 列 = 一切() )
【问题讨论】: