こちらの続きです。
![](https://www.saecanet.com/wp-content/uploads/2024/08/ogasuta458A8136_TP_V4-160x90.jpg)
library(gt)を利用して取り急ぎテーブルを出力するコード
...
gt {gt} で作成したテーブルのセルで ハイパーリンク を表示します。
始めにサンプルデータを作成します。
<- c("quarto", "RStudio", "R Project", "Positron")
websites <- c("https://quarto.org/", "https://posit.co/products/open-source/rstudio/", "https://www.r-project.org/", "https://github.com/posit-dev/positron")
urls <- paste0('<a href="', urls, '" target="_blank">', urls, "</a>")
hyperlinks <- data.frame(websites, hyperlinks)
sample.df 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 |
以上です。