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

时间:2015-12-14 19:10:05

标签: r markdown 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()
}

1 个答案:

答案 0 :(得分:3)

根据 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}
```