在没有其他文本的情况下将链接添加到GT表元素

发布于 2025-02-05 14:08:15 字数 623 浏览 2 评论 0原文

我想将链接添加到我的GT表单元格中,如下所示:

raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
tibble(
  name = raw_dat$id,
  link = paste(raw_dat$mpg, "And <a href = 'https://www.cars.com//'>here</a>")) %>%
  mutate(link = map(link, gt::html)) %>%
  gt

”在此处输入图像描述

我如何使链接落在raw_dat $ mpg 上,并且之后没有任何其他文本。因此,所需的输出是单击raw_dat $ mpg单元格,您可以将其带到cars.com。

I want to add links to my gt table cells as shown:

raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
tibble(
  name = raw_dat$id,
  link = paste(raw_dat$mpg, "And <a href = 'https://www.cars.com//'>here</a>")) %>%
  mutate(link = map(link, gt::html)) %>%
  gt

Results:

enter image description here

How can I make the link fall on the raw_dat$mpg and not have any additional text after it. So the desired output is by clicking on the raw_dat$mpg cell, you can be taken to cars.com.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2025-02-12 14:08:15

您可以使用以下代码:

library(dplyr)
library(gt)
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
df <- tibble(
  name = raw_dat$id,
  link =  'https://www.cars.com//')

df %>%
  mutate(link = sprintf('<p><a href = "%s">%s</a>', link, raw_dat$mpg),
         link = map(link, gt::html)) %>%
  gt

输出:

”在此处输入图像描述

You can use the following code:

library(dplyr)
library(gt)
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
df <- tibble(
  name = raw_dat$id,
  link =  'https://www.cars.com//')

df %>%
  mutate(link = sprintf('<p><a href = "%s">%s</a>', link, raw_dat$mpg),
         link = map(link, gt::html)) %>%
  gt

Output:

enter image description here

夏九 2025-02-12 14:08:15

也许您可以使用sprintf构造字符串(显然未测试)。

link = sprintf("<a href = 'https://www.cars.com'>%s</a>", raw_dat$mpg)

Perhaps you can construct a string using sprintf (not tested, obviously).

link = sprintf("<a href = 'https://www.cars.com'>%s</a>", raw_dat$mpg)
葮薆情 2025-02-12 14:08:15

将两个现有解决方案融合在一起。这是一个笔直的tidyverse方法:

library(tidyverse)
library(gt)

mtcars %>% 
  slice(1:15) %>% 
  rownames_to_column("name") %>% 
  mutate(link =  'https://www.cars.com//',
         link = sprintf('<p><a href = "%s">%s</a>', link, raw_dat$mpg),
         link = map(link, gt::html)) %>%
  select(name, link) %>% 
  gt


..... .....

Taking together both existing solution. Here is a straight tidyverse approach:

library(tidyverse)
library(gt)

mtcars %>% 
  slice(1:15) %>% 
  rownames_to_column("name") %>% 
  mutate(link =  'https://www.cars.com//',
         link = sprintf('<p><a href = "%s">%s</a>', link, raw_dat$mpg),
         link = map(link, gt::html)) %>%
  select(name, link) %>% 
  gt

enter image description here

.....
.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文