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("first line\n")</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("second line\n")</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?
A simple hack would be to surround your chunk with {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With