R 笔记本 HTML 格式 - 添加超链接到分页 table
R Notebook HTML Format - add hyperlinks to paged table
我希望从包含带超链接的分页 table 的 R Notebook 编织一个 html 文件。
可以使用 knitr::kable
插入超链接,但我找不到生成 paged table 的方法功能。
分页 tables 是默认的笔记本输出,但我找不到插入功能性 超链接 的方法。非常感谢您的帮助。
---
title: "Paged notebook table with hyperlinks"
output:
html_notebook:
code_folding: "hide"
---
```{r rows.print=3}
wiki.url <- "https://en.wikipedia.org/wiki/"
df1 <- data.frame(Month=month.name, URL=paste0("[", month.name, "](", wiki.url, month.name, ")"))
df2 <- data.frame(Month=month.name, URL=paste0("<a href='", wiki.url, month.name, "'>", month.name, "</a>"))
print(df1)
```
```{r rows.print=3}
print(df2)
```
```{r rows.print=3}
knitr::kable(df1)
```
```{r rows.print=3}
knitr::kable(df2)
```
由于我的问题似乎没有完美的解决方案,我想我会 post 我想出的解决方法 - 以防有人遇到类似的问题。
我创建了 table 加上带有 knitr::kable
的超链接,然后添加了一个 html 按钮和 inline javascript 来切换可见性 - 不像分页 table 那样优雅,但是这份工作。
请注意文件底部的 <script>
标记,默认情况下 hides tables。
(将代码粘贴到 RStudio 中的 .Rmd 文件中):
---
title: "Managing large tables with hyperlinks in html notebook"
output:
html_notebook:
code_folding: "hide"
---
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
```{r}
library(knitr)
df1 <- data.frame(Month=month.name, Link=paste0("[", month.name, "](https://en.wikipedia.org/wiki/", month.name, ")"))
```
<button class="button" onclick="myFunction('DIV_months')">Show/hide table</button>
<div id="DIV_months" class="div_default_hide">
```{r}
knitr::kable(df1)
```
</div>
<script>
var divsToHide = document.getElementsByClassName("div_default_hide");
for(var i = 0; i < divsToHide.length; i++)
{
divsToHide[i].style.display = 'none';
}
</script>
我希望从包含带超链接的分页 table 的 R Notebook 编织一个 html 文件。
可以使用 knitr::kable
插入超链接,但我找不到生成 paged table 的方法功能。
分页 tables 是默认的笔记本输出,但我找不到插入功能性 超链接 的方法。非常感谢您的帮助。
---
title: "Paged notebook table with hyperlinks"
output:
html_notebook:
code_folding: "hide"
---
```{r rows.print=3}
wiki.url <- "https://en.wikipedia.org/wiki/"
df1 <- data.frame(Month=month.name, URL=paste0("[", month.name, "](", wiki.url, month.name, ")"))
df2 <- data.frame(Month=month.name, URL=paste0("<a href='", wiki.url, month.name, "'>", month.name, "</a>"))
print(df1)
```
```{r rows.print=3}
print(df2)
```
```{r rows.print=3}
knitr::kable(df1)
```
```{r rows.print=3}
knitr::kable(df2)
```
由于我的问题似乎没有完美的解决方案,我想我会 post 我想出的解决方法 - 以防有人遇到类似的问题。
我创建了 table 加上带有 knitr::kable
的超链接,然后添加了一个 html 按钮和 inline javascript 来切换可见性 - 不像分页 table 那样优雅,但是这份工作。
请注意文件底部的 <script>
标记,默认情况下 hides tables。
(将代码粘贴到 RStudio 中的 .Rmd 文件中):
---
title: "Managing large tables with hyperlinks in html notebook"
output:
html_notebook:
code_folding: "hide"
---
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
```{r}
library(knitr)
df1 <- data.frame(Month=month.name, Link=paste0("[", month.name, "](https://en.wikipedia.org/wiki/", month.name, ")"))
```
<button class="button" onclick="myFunction('DIV_months')">Show/hide table</button>
<div id="DIV_months" class="div_default_hide">
```{r}
knitr::kable(df1)
```
</div>
<script>
var divsToHide = document.getElementsByClassName("div_default_hide");
for(var i = 0; i < divsToHide.length; i++)
{
divsToHide[i].style.display = 'none';
}
</script>