Home > Net >  R markdown automatic line break
R markdown automatic line break

Time:11-20

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?

CodePudding user response:

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")
  • Related