使用knitr / R markdown编织时,是否可以在代码中隐藏一些注释?例如:
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r}
# Generate some data
rnorm(2)
## But keep this comment
```
在编织时,我希望第一个评论消失,但保留第二个不知何故。
答案 0 :(得分:7)
这是一个修改钩子以改变编织者行为的快速示例。
---
title: "SOSO"
author: "SO"
date: 2017-06-06
output: pdf_document
---
```{r setup-hook, echo=FALSE}
hook_in <- function(x, options) {
x <- x[!grepl("^#\\s+", x)]
paste0("```r\n",
paste0(x, collapse="\n"),
"\n```")
}
knitr::knit_hooks$set(source = hook_in)
```
```{r}
# Generate some data
# Lines that starts with `# ` will be removed from the rendered documents
rnorm(2)
## But keep this comment
## But lines that starts with `## ` will be kept
```
答案 1 :(得分:2)
实际上,您可以通过将数字索引传递给块选项echo
来选择显示任何R代码行,例如
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r echo=4:7}
# Generate some data
rnorm(2)
## But keep this comment
```
有关详细信息,请参阅knitr documentation。