Prevent Knitr/Rmarkdown from Interleaving Chunk Output with Code

prevent knitr/Rmarkdown from interleaving chunk output with code

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')
```

In R Markdown is there an option for code evaluation that groups all of a code block together and then all of the outputs?

You can use the results = "hold" argument in the chunk options.

```{r, results = "hold"}
("one line")
("second line")
```

Sample Image

Can markdown expressions and results be interleaved in the same block?

This can be done using hooks. Add the following code chunk right at the top of your Rmd document. It uses the document hook which is run on the md file at the last stage of knitting. The hook defined below identifies subsequent code chunks without any text chunk in between and collapses it into one.

```{r setup, cache = F, echo = F}
knitr::knit_hooks$set(document = function(x){
gsub("```\n*```r*\n*", "", x)
})
```

NOTE. It is important to set cache = F in this chunk so that this code is always run.

Separate with spaces to output chunk pieces RMarkdown

You want to include in the output both the R code and the results. The inclusion of additional commands in R code will bring them to the output as well. Thus, you may consider splitting of R chunk into several pieces in order to add space by means of tex.
Here are the three options to add space between code chunks.

## Generamos  modelo (CAFEINA incluído)
```{r tidy=TRUE}
(modelo<-randomForest(diagnostico~.,data=datos.entreno))
```
  <!-- Option #1 -->
 
```{r tidy=TRUE}
# La importancia de las variables
vimp<-as.data.frame(modelo$importance)
vimp[order(vimp$MeanDecreaseGini),,drop=FALSE]
```
\leavevmode
\newline <!-- Option #2 -->
```{r tidy=TRUE}
# Hacer predicciones
predicciones <- predict(modelo, datos.test)
predicciones
```
\vspace{30mm} <!-- Option #3 -->
```{r tidy=TRUE}
# Matriz de confusión
(mc <- with(datos.test,table(predicciones, diagnostico)))
```

Rmarkdown & {knitr}: code chunk highlights in lists?

Ok, got it working. For some reason hitting tab (four spaces) twice in R Studio causes the issue above. I suspect it has something to do with R Studio rather than R Markdown or {knitr}. The solution is this response by @Yihui, and relies on using the indent parameter. That will keep the highlight and recognition of the chunk as well as keeping everything aligned in the output.

I never thought to use the indent parameter as it's not listed in the {knitr} chunk options in the current R Markdown Reference Guide (here), and is only mentioned passively at the end of the Code Description section in the {knitr} chunk options (here).



Related Topics



Leave a reply



Submit