在R markdown中隐藏评论

MLE*_*LEN 2 markdown latex r knitr

使用knitr/R markdown编织时,是否可以在代码中隐藏一些注释?例:

---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document


---

```{r}

# Generate some data

rnorm(2)

## But keep this comment

```
Run Code Online (Sandbox Code Playgroud)

在编织时,我希望第一个评论消失,但保留第二个不知何故.

sin*_*eso 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

```
Run Code Online (Sandbox Code Playgroud)

产生这个在此输入图像描述


Yih*_*Xie 5

实际上,您可以通过将数字索引传递给块选项来选择显示任何 R 代码行echo,例如

---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---

```{r echo=4:7}

# Generate some data

rnorm(2)

## But keep this comment

```
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅knitr文档