Create interactive links in gt table (in rmarkdown)

You can use the gt helpers to format html or md content inside a table

Example

library(gt)
library(tidyverse)

df <- tibble(
  country = c("UK", "US"),
  name = c("BBC", "CNN"),
  link = c("https://www.bbc.com/news", "https://edition.cnn.com/"))
  
# using html
df %>%
    mutate(
        link = map(link, ~ htmltools::a(href = .x, "website")),
        link = map(link, ~ gt::html(as.character(.x)))) %>%
    gt()

country

name

link

UK

BBC

website

US

CNN

website


# using markdown
df %>%
    mutate(
        link = glue::glue("[website]({link})"),
        link = map(link, gt::md)) %>%
    gt()

country

name

link

UK

BBC

website

US

CNN

website

Created on 2020-06-18 by the reprex package (v0.3.0.9001)

  • html() will tell gt to deal with the text as html content. You can create the text yourself. I used htmltools helper
  • md() will tell get to consider the text as markdown, and it will process it to render as html.

Hope it helps

3 Likes