0

I have made a custom function which creates a couple of strings. I would like to have these strings displayed on two lines (the first above the other). This is my current code:

---
output:
  bookdown::pdf_document2:
      toc: false
---
```{r echo = FALSE}
b <- function(d){
  x <- paste("foo")
  y <- paste("bar")
  
  paste(x, "\n", y)
  
  
}
```
`r b()`

`r paste("this", "\n", "does", "\n", "work")`

However, while foo bar is shown, it is only inline, not on different lines.

I have also tried the following: paste0(x, "\n", y) cat(x, "\n", y) paste(x, y, sep="\n") paste0(x, y, sep="\n") cat(x, y, sep="\n") None of these work inside the function. cat() actually doesn't even show anything when inside the function. What am I missing/doing wrong?

MJL
  • 161
  • 7
  • 3
    The [rules of markdown says you need two spaces at the end of lines](https://stackoverflow.com/questions/30057110/line-breaks-in-r-markdown-text-not-code-blocks) if you want to force a line break. Try `r paste("this", " \n", "might", " \n", "work")` – MrFlick May 26 '21 at 03:46
  • Whoa, that's an "obscure" feature I've never heard of. Nice find. – Roman Luštrik May 26 '21 at 05:25
  • Will not judge on the "obscurity" level as I came across it when reading a bit about (R)markdown. Using `inline` for multi-line output is a bit "less" inline :). Check out https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html and how you can programmatically output text from a chunk using the chunk option `result="asis"`. – Ray May 26 '21 at 05:43
  • @MrFlick Thanks, this worked! If you post this as an answer I'll mark it correct. – MJL May 28 '21 at 02:36

0 Answers0