Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chunk option `results="hold"` in Rstudio notebook has no effect

I am trying to use the chunk option results="hold" to collect together the output of several commands in one chunk of an R notebook. Here is an example notebook which shows the problem:

---
title: "R Notebook"
output: html_notebook
---

```{r, results="hold"}
cat("first line\n")
cat("second line\n")
```

According to the knitr manual, the results="hold" option means to "hold all the output pieces and push them to the end of a chunk". From this, I hoped that I would first get both lines of R code, followed by the two lines of output. Instead, when I press the "Preview" button in R studio, I see the output of the first cat() before the second cat() command is shown. The generated HTML output, from the .nb.html file, is

<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuY2F0KFwiZmlyc3QgbGluZVxcblwiKVxuYGBgIn
<pre class="r"><code>cat(&quot;first line\n&quot;)</code></pre>
<!-- rnb-source-end -->
<!-- rnb-output-begin eyJkYXRhIjoiZmlyc3QgbGluZVxuIn0= -->
<pre><code>first line</code></pre>
<!-- rnb-output-end -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuY2F0KFwic2Vjb25kIGxpbmVcXG5cIilcbmBgYC
<pre class="r"><code>cat(&quot;second line\n&quot;)</code></pre>
<!-- rnb-source-end -->
<!-- rnb-output-begin eyJkYXRhIjoic2Vjb25kIGxpbmVcbiJ9 -->
<pre><code>second line</code></pre>
<!-- rnb-output-end -->
<!-- rnb-chunk-end -->

How can I collect all output of the chunk together, after all of the commands?

like image 995
jochen Avatar asked Jan 28 '26 06:01

jochen


1 Answers

A simple hack would be to surround your chunk with {}

like image 60
Alix Avatar answered Jan 31 '26 22:01

Alix