本文介绍了如何在RMarkdown中隐藏代码,并带有查看选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个RMarkdown 文档,我想在其中重新运行一些代码( 5至9).无需再次显示这些块,因此我考虑使用

I'm writing an RMarkdown document in which I'd like to re-run some chunks (5 to 9).There's no need to display these chunks again, so I considered using

```{r echo=FALSE}

使重新运行的块不可见,如另一个.很好,并且可以输出所需的结果(提高了第二次迭代的拟合度-请在此处实施此解决方案).

to make the rerun chunks invisible, as described in another stackoverflow question. This is fine, and outputs the desired results (improved fit of second iteration - see this solution implemented here).

但是,在理想的世界中,该代码将是可扩展的,因此用户出于教育目的和清晰性的目的,可以准确地看到正在发生的事情(例如,请参阅Greasemonkey解决方案的链接),而不是像我在第二个rpubs示例中那样隐藏.该解决方案可能看起来像这样,但周围的方框较短,以避免分心:

In an ideal world, however, the code would be expandable so the user could see exactly what's going on if they want to for educational purposes and clarity (e.g. see link to Greasemonkey solution here) rather than hidden as in my second rpubs example. The solution may look something like this, but with a shorter surrounding box to avoid distraction:

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解决方案感到满意,但使用一个可扩展的代码片段会更满意.

I'm happy with the echo=F solution, but would be even happier with an expandable code snippet one.

除第一个示例外,所有RPubs示例现在都已删除,以避免用基本相同的文档阻塞其优秀的发布系统.

推荐答案

使用 rmarkdown可以使此操作变得更加容易 软件包,该软件包三年前不存在.基本上,您只需要打开代码折叠"即可: http://rmarkdown.rstudio.com/html_document_format.html #code_folding .您不再需要编写任何JavaScript.

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.

例如

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

这篇关于如何在RMarkdown中隐藏代码,并带有查看选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:17
查看更多