如何隐藏RMarkdown中的代码,可以选择查看它

时间:2013-01-02 18:38:44

标签: r markdown code-snippets rstudio

我正在写一个RMarkdown document,其中我想重新运行一些块(5到9)。 没有必要再次显示这些块,所以我考虑使用

```{r echo=FALSE}

使重新运行的块不可见,如另一个stackoverflow question中所述。这很好,并输出所需的结果(改进了第二次迭代的拟合 - 请参阅此解决方案实现here)。

然而,在一个理想的世界中,代码是可扩展的,因此如果用户希望出于教育目的和清晰度(例如,请参阅Greasemonkey解决方案here的链接)而不是隐藏为在我的第二个rpubs示例中。解决方案可能看起来像这样,但周围的盒子较短,以避免分心:

for (i in 1:nrow(all.msim)){ # Loop creating aggregate values (to be repeated later)
  USd.agg[i,]   <- colSums(USd.cat * weights0[,i])
}

for (j in 1:nrow(all.msim)){
weights1[which(USd$age <= 30),j] <- all.msim[j,1] /USd.agg[j,1] 
weights1[which(USd$age >= 31 & USd$age <= 50),j] <- all.msim[j,2] /USd.agg[j,2] 
weights1[which(USd$age >= 51),j] <- all.msim[j,3] /USd.agg[j,3] ## 
}
# Aggregate the results for each zone
for (i in 1:nrow(all.msim)){
  USd.agg1[i,]   <- colSums(USd.cat * weights0[,i] * weights1[,i])
}
# Test results 
for (j in 1:nrow(all.msim)){
weights2[which(USd$sex == "m"),j] <- all.msim[j,4] /USd.agg1[j,4]  
weights2[which(USd$sex == "f"),j] <- all.msim[j,5] /USd.agg1[j,5] 
}

for (i in 1:nrow(all.msim)){
USd.agg2[i,]   <- colSums(USd.cat * weights0[,i] * weights1[,i] * weights2[,i])
}

for (j in 1:nrow(all.msim)){
weights3[which(USd$mode == "bicycle"),j] <- all.msim[j,6] /USd.agg2[j,6]  
weights3[which(USd$mode == "bus"),j] <- all.msim[j,7] /USd.agg2[j,7] 
weights3[which(USd$mode == "car.d"),j] <- all.msim[j,8] /USd.agg2[j,8]  
weights3[which(USd$mode == "car.p"),j] <- all.msim[j,9] /USd.agg2[j,9]
weights3[which(USd$mode == "walk"),j] <- all.msim[j,10] /USd.agg2[j,10]
}
weights4 <- weights0 * weights1 * weights2 * weights3
for (i in 1:nrow(all.msim)){
USd.agg3[i,]   <- colSums(USd.cat * weights4[,i])
}
# Test results 
plot(as.vector(as.matrix(all.msim)), as.vector(as.matrix(USd.agg3)),
     xlab = "Constraints", ylab = "Model output")
abline(a=0, b=1)
cor(as.vector(as.matrix(all.msim)), as.vector(as.matrix(USd.agg3)))
#rowSums(USd.agg3[,1:3]) # The total population modelled for each zone, constraint 1
#rowSums(USd.agg3[,4:5])
#rowSums(USd.agg3[,6:10])

我对echo=F解决方案感到满意,但对于可扩展的代码段,我会更高兴。

编辑:除了第一个以外的所有RPub示例现已被删除,以避免使用基本相同的文档堵塞其优秀的出版物系统。

2 个答案:

答案 0 :(得分:36)

This has been made much easier with the rmarkdown package, which did not exist three years ago. Basically you just turn on "code folding": http://rmarkdown.rstudio.com/html_document_format.html#code_folding. You no longer have to write any JavaScript.

E.g.

---
title: "Habits"
output:
  html_document:
    code_folding: hide
---

答案 1 :(得分:9)

如果你在代码之前添加一个html标签,你可以使用CSS选择器对输出的位做一些聪明的事情 - markdown轻松地通过HTML传递:

<style>
div.hidecode + pre {display: none}
</style>

<div class="hidecode"></div>
```{r}
summary(cars)
```

此处,我的CSS样式规则与<pre>后的第一个<div class=hidecode>标记匹配,并将其设置为不可见。 Markdown用两个<pre>标签写入R块 - 一个用于R,一个用于输出,这个CSS捕获第一个。

现在你知道如何匹配CSS中的代码和输出块,你可以在Javascript中用它们做各种聪明的事情。您可以在<div class=hidecode>标记中放置一些内容,然后添加一个切换可见性的点击事件:

<style>
div.hidecode + pre {display: none}
</style>
<script>
doclick=function(e){
e.nextSibling.nextSibling.style.display="block";
}
</script>

<div class="hidecode" onclick="doclick(this);">[Show Code]</div>
```{r}
summary(cars)
```

复杂性的下一步是让动作切换,但是你也可以使用jQuery并获得真正的时髦。或者使用这种简单的方法。让我们用一个按钮来做,但是你还需要一个div来让你的钩子进入R命令PRE块,并且遍历变得有点复杂:

<style>
div.hideme + pre {display: none}
</style>
<script>
doclick=function(e){
code = e.parentNode.nextSibling.nextSibling.nextSibling.nextSibling
if(code.style.display=="block"){
 code.style.display='none';
 e.textContent="Show Code"
}else{
 code.style.display="block";
 e.textContent="Hide Code"
}
}
</script>

<button class="hidecode" onclick="doclick(this);">Show Code</button>
<div class="hideme"></div>
```{r}
summary(cars)
```

(注意:我认为你可以在<div>标签中包装R块:

<div class="dosomething">
```{r}
summary(cars) 
``` 
</div>

但失败了 - 任何人都知道为什么?)