0

I have a problem with knitting in R. If a chunk has something that needs to be printed, the chunk gets split in two and the result is shown under each piece of code, as you can see in the following image:
enter image description here
So, I want every result to be shown together at the end of the chunk. Is there a way to do that?
Something like the following:

```{r echo=T, eval=FALSE}
message("hello")
message("world")
```
\#\# hello  
\#\# world

enter image description here Thank you very much.

  • 2
    Welcome to Stack Overflow. It's easier to help you if you make your question reproducible including your code and sample date that can be used to test and verify possible solutions Check out [How to make a great r reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Peter Aug 23 '21 at 13:40

2 Answers2

1

Use collapse=TRUE, documented here: https://yihui.org/knitr/options/#code-evaluation.

---
title: hello
---

```{r blockname}
message("hello")
message("world")
```

rmarkdown chunk without collapse

---
title: hello
---

```{r blockname, collapse = TRUE}
message("hello")
message("world")
```

rmarkdown chunk with collapse

Since you need to change the way R shows things (output after a command), then we need to borrow from a previous answer of mine:

---
title: hello
---

```{r blockname, echo = FALSE, include = FALSE}
message("hello")
message("world")
```

```{r showblockname, ref.label='blockname', eval=FALSE}
```

```{r blockname, echo=FALSE, collapse=TRUE}
```

rmarkdown chunk, shifted-output and collapsed

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • I actually wanted to do the following: Chunk: message("hello") message("world") Result: ##hello ##world – NIKOLAOS RAPANIS Aug 23 '21 at 13:48
  • Okay, that's much clearer than your current question. Perhaps you should have included a reproducible example? – r2evans Aug 23 '21 at 13:51
  • I have included a picture above. There, you can see what I mean. – NIKOLAOS RAPANIS Aug 23 '21 at 14:02
  • I think this addresses your question. In the future, Peter's comment about making a question reproducible is a good thing for at least three reasons: (a) clarity: working code demonstrating the misbehavior and then seeing the desired output can be really good for us, since the problem is often perfectly-clear only in the head of the OP; (b) courtesy: you are asking for help, the impetus is on *you* to make that as easy for us as possible to help you; (c) speed of answers: the easier it is for us to pick up and "play", the faster you are likely to get constructive comments and/or answers. – r2evans Aug 23 '21 at 14:11
  • I understand. That's why I added the piece of code, thanks. – NIKOLAOS RAPANIS Aug 23 '21 at 14:16
  • For clarity ... you did not add pieces of code, you added images of code, the two are not the same: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please just include the code next time. Thanks! – r2evans Aug 23 '21 at 15:04
1

It's very simple. You just need to set a chunk option results = "hold".

For example:

knitr::opts_chunk$set(echo = TRUE, results = "hold")
X = rnorm(100, 100, 15)
Y = rnorm(100, 100, 20)

mean(Y+X)
mean(Y)+mean(X)