1

In R markdown, a code that is too long goes off the page. For example, the following code is not reported completely.

---
title: "one two three"
author: "ABC"
date: "2022-11-19"
output: pdf_document
---
```{r eval = F}
tmp <- c(123123123123123123, 123123123123123, 123213123123123, 123123123123, 123123213123123123123, 123123123)
```

Of course, the best situation is to make the code shorter, sometimes it is not possible, because the code is written by other authors and there are thousands of lines that go off the page.

In this case, is it possible to break the lines automatically?

r2evans
  • 141,215
  • 6
  • 77
  • 149
M.C. Park
  • 295
  • 1
  • 10
  • 1
    Likely a dupe of https://stackoverflow.com/q/33481271/3358272. Try installing `formatR` and setting `tidy.opts` as in [this answer](https://stackoverflow.com/a/66753995/3358272). – r2evans Nov 19 '22 at 17:55

1 Answers1

1

The global knitr option raised by @r2evans with the additional dependency on formatR package is one of the solutions.

knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 60), tidy = TRUE)

another solution for the pdf output (which you ar using) can be to use header-includes: setup with optional usage of styler package.

---
title: "R Notebook"
output: pdf_document
header-includes:
  - |
    ```{=latex}
    \usepackage{fvextra}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
      breaksymbolleft={}, 
      showspaces = false,
      showtabs = false,
      breaklines,
      commandchars=\\\{\}
    }
    ```
---

```{r}
knitr::opts_chunk$set(tidy="styler")
polkas
  • 3,797
  • 1
  • 12
  • 25