Here is the solution I proposed
Chunk Output
========================================================
Outside a chunk.
```{r chunk1, results='hide'}
cat('Inside a chunk\n\n')
for (i in 1:3) {
cat('* Inside loop #', i, '\n')
}
cat('Outside a loop, but still inside the first chunk')
```
```{r ref.label = 'chunk1', results = 'asis', echo = F}
```
In the latest version of knitr, @yihui has added a new chunk option results = "hold"
, which automatically holds printing of all output to the end. Accordingly, we can just write
Chunk Output
========================================================
Outside a chunk.
```{r chunk1, results='hold'}
cat('Inside a chunk\n\n')
for (i in 1:3) {
cat('* Inside loop #', i, '\n')
}
cat('Outside a loop, but still inside the first chunk')
```