R Notebook HTML格式 - 添加到分页表的超链接 [英] R Notebook HTML Format - add hyperlinks to paged table

查看:206
本文介绍了R Notebook HTML格式 - 添加到分页表的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从包含带超链接的分页表的R笔记本编织一个html文件。

可以使用 knitr :: kable 插入超链接,但我找不到使用此函数生成 分页 表的方法。

分页表是默认的笔记本输出,但我可以找不到插入功能性 超链接 的方法。非常感谢你的帮助。

I wish to knit an html file from an R Notebook that contains paged tables with hyperlinks.
Hyperlinks can be inserted using knitr::kable, but I can't find a way to generate a paged table with this function.
Paged tables are the default notebook output, but I can't find a way of inserting functional hyperlinks. Many thanks for your help.

---
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)
```


推荐答案

由于似乎没有一个完美的解决方案来解决我的问题,我想我会发布我想出的解决方法 - 以防有人遇到类似的问题。

我用 knitr :: kable 然后添加了一个html按钮和内联javascript 切换可见性 - 不像分页表那样优雅,但可以完成工作。

注意< script> 默认情况下隐藏表格的文件底部的标记。$
将代码粘贴到RStudio中的.Rmd文件):

Since there doesn't seem to be a perfect solution to my problem, I thought I'd post the workaround that I came up with - in case someone has a similar problem.
I created the table plus hyperlinks with knitr::kable and then added an html button and inline javascript to toggle visibility - not as elegant as a paged table, but does the job.
Note the <script> tag at the bottom of the file that hides tables by default.
(Paste code into an .Rmd file in RStudio):

---
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>

这篇关于R Notebook HTML格式 - 添加到分页表的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆