交互式 show/hide 代码 R Markdown/Knitr 报告
Interactively show/hide code R Markdown/Knitr report
有没有办法在 R Markdown/Knitr 报告中 show/hide 交互式编码?
如果我理解正确的话,你至少可以通过使用 HTML 输出来做到这一点,就像这个最小的例子:
---
title: "Toggle Code boxes"
output: html_document
date: "January 12, 2016"
---
First add the javascript to toggle boxes(remember to indent it)
<script language="javascript">
function toggle(num) {
var ele = document.getElementById("toggleText" + num);
var text = document.getElementById("displayText" + num);
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "show";
}
else {
ele.style.display = "block";
text.innerHTML = "hide";
}
}
</script>
and then we have some R code with the toggle button wrapped around (also indented):
<a id="displayText" href="javascript:toggle(1);">Show underlying code</a>
<div id="toggleText1" style="display: none">
```{r}
x <- sample(100)
mean.x <- mean(x)
```
</div>
The mean is `r mean.x`. Please click the link to see the source code.
<a id="displayText" href="javascript:toggle(2);">Show underlying code</a>
<div id="toggleText2" style="display: none">
```{r}
median.x <- median(x)
```
</div>
And the median is `r median.x`. Please click the link to see the source code.
有没有办法在 R Markdown/Knitr 报告中 show/hide 交互式编码?
如果我理解正确的话,你至少可以通过使用 HTML 输出来做到这一点,就像这个最小的例子:
---
title: "Toggle Code boxes"
output: html_document
date: "January 12, 2016"
---
First add the javascript to toggle boxes(remember to indent it)
<script language="javascript">
function toggle(num) {
var ele = document.getElementById("toggleText" + num);
var text = document.getElementById("displayText" + num);
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "show";
}
else {
ele.style.display = "block";
text.innerHTML = "hide";
}
}
</script>
and then we have some R code with the toggle button wrapped around (also indented):
<a id="displayText" href="javascript:toggle(1);">Show underlying code</a>
<div id="toggleText1" style="display: none">
```{r}
x <- sample(100)
mean.x <- mean(x)
```
</div>
The mean is `r mean.x`. Please click the link to see the source code.
<a id="displayText" href="javascript:toggle(2);">Show underlying code</a>
<div id="toggleText2" style="display: none">
```{r}
median.x <- median(x)
```
</div>
And the median is `r median.x`. Please click the link to see the source code.