在 R Knitr 中将代码拆分为几个语法无效的块

Split code into several syntactically invalid chunks in R Knitr

是否可以在代码块中获取非格式化的降价文本?我的目标是在 for 循环中显示描述。在下面的例子中,这样的操作导致将代码分成两个语法上无效的块:

I am using for cycle here
```{r results='hide'}
for(i in 1:5){
  foo()
```

There is a lot of interesting stuff inside
```{r results='hide'}
  bar()
}
```

理想情况下会生成:

我这里用的是循环

for(i in 1:5){
  foo()

里面有很多有趣的东西

  bar()
}

根据 user2706569 的评论建议,您可以使用名称定义一次代码块并对其进行评估。然后你可以重用代码块,但只回显你想要的行,而不进行评估。

取自Yihui's examples...

The plots from the original evaluation are 
shown, but the code is not echoed here. (To 
omit all of the outputs, check out the 
chunk options such as `include=FALSE`.)

```{r mychunk, echo=FALSE}
## 'ugly' code that I do not want to show
par(mar = c(4, 4, 0.1, 0.1), cex.lab = 0.95, cex.axis = 0.9,
    mgp = c(2, 0.7, 0), tcl = -0.3)
plot(mtcars[, 1:2])
plot(mtcars[, 4:5])

```

Now describe the code as you wish without evaluation.

Here's the first and second lines from the original chunk.

```{r mychunk, echo=1:2, eval=FALSE}
```

Here's the third line.

```{r mychunk, echo=3, eval=FALSE}
```

Here's the fourth line.

```{r mychunk, echo=4, eval=FALSE}
```

Here's the fifth line.

```{r mychunk, echo=5, eval=FALSE}
```