Using the function gt in R, I can get hyperlinks working for cells in the table, but I can't quite figure out how to do it for column names.
library(gt)
library(tidyverse)
make_into_links <- function(vec){
clean_vec <- str_replace_all(vec[-1], ":", "")%>%
str_replace_all(" ", "+")
link <- paste0("https://www.google.com/search?q=", clean_vec, "&btnI")
column_names <- paste0("<a href=", link, ">", vec[-1], "</a>")
column_names <- as.character(c(vec[1], column_names))
}
df <- tibble(
id_col = 1:2,
`usa: some other text` = rnorm(2),
`uk: some other text` = rnorm(2)
)
colnames(df) <- make_into_links(colnames(df))
gt(df)
Created on 2023-06-26 with reprex v2.0.2
You can use markdown with md()
. From the documentation:
Using the countrypops dataset again, we label columns similarly to before but this time making the column labels be bold through Markdown formatting (with the md() helper function). It's possible here to use either a = or a ~ between the column name and the label text.
dplyr::filter(country_name == "Mongolia") |> tail(5) |> gt() |> cols_label( country_name = md("**Name**"), year = md("**Year**"), population ~ md("**Population**") )
In your example, the following works:
gt(df) %>%
cols_label(
`usa: some other text` = md("[Link text Here](https://link-url-here.org)")
)