gt {gt} のセルにハイパーリンクを表示する方法

こちらの続きです。

library(gt)を利用して取り急ぎテーブルを出力するコード
...

gt {gt} で作成したテーブルのセルで ハイパーリンク を表示します。

始めにサンプルデータを作成します。

websites <- c("quarto", "RStudio", "R Project", "Positron")
urls <- c("https://quarto.org/", "https://posit.co/products/open-source/rstudio/", "https://www.r-project.org/", "https://github.com/posit-dev/positron")
hyperlinks <- paste0('<a href="', urls, '" target="_blank">', urls, "</a>")
sample.df <- data.frame(websites, hyperlinks)
sample.df
   websites
1    quarto
2   RStudio
3 R Project
4  Positron
                                                                                                                   hyperlinks
1                                                       <a href="https://quarto.org/" target="_blank">https://quarto.org/</a>
2 <a href="https://posit.co/products/open-source/rstudio/" target="_blank">https://posit.co/products/open-source/rstudio/</a>
3                                         <a href="https://www.r-project.org/" target="_blank">https://www.r-project.org/</a>
4                   <a href="https://github.com/posit-dev/positron" target="_blank">https://github.com/posit-dev/positron</a>

gt {gt} でテーブルを作成します。

library(gt)
library(dplyr)
gt(data = sample.df) %>%
  tab_style(
    style = cell_text(align = "center", whitespace = "nowrap"),
    locations = cells_column_labels(columns = everything())
  ) %>%
  tab_style(
    style = cell_text(whitespace = "nowrap"),
    locations = cells_body(columns = everything())
  )
websiteshyperlinks
quarto<a href="https://quarto.org/" target="_blank">https://quarto.org/</a>
RStudio<a href="https://posit.co/products/open-source/rstudio/" target="_blank">https://posit.co/products/open-source/rstudio/</a>
R Project<a href="https://www.r-project.org/" target="_blank">https://www.r-project.org/</a>
Positron<a href="https://github.com/posit-dev/positron" target="_blank">https://github.com/posit-dev/positron</a>

マークアップ がそのまま表示されてしまいます。

そこで、関数 fmt_markdown {gt} を利用します。

gt(data = sample.df) %>%
  fmt_markdown(columns = "hyperlinks", md_engine = "markdown") %>%
  tab_style(
    style = cell_text(align = "center", whitespace = "nowrap"),
    locations = cells_column_labels(columns = everything())
  ) %>%
  tab_style(
    style = cell_text(whitespace = "nowrap"),
    locations = cells_body(columns = everything())
  )
websiteshyperlinks
quarto

https://quarto.org/

RStudio

https://posit.co/products/open-source/rstudio/

R Project

https://www.r-project.org/

Positron

https://github.com/posit-dev/positron

以上です。