こちらの続きです。

library(gt)を利用して取り急ぎテーブルを出力するコード
library(gt) を利用して 取り急ぎ テーブルを出力するコードです。以下の条件のテーブルを出力します。行番号 を表示。タイトルは 太字イタリック、サブタイトルは 太字、キャプションは イタリック。2列目の数値には 符号 を添付し、小...
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())
)| websites | hyperlinks |
|---|---|
| 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())
)| websites | hyperlinks |
|---|---|
| quarto | |
| RStudio | |
| R Project | |
| Positron |
以上です。

