48

I currently have some code that looks like so:

```{r, tidy=TRUE}
plot(DT$age, DT$height, xlab = "Age of participant in Trials", ylab = "Height of participant in       Trials")
```

Now, it was my understanding that setting tidy to TRUE would make it so that when I knit the code together, the code would not go running off the page and would wrap by itself. However, I sporadically still get run off source code displays when I do commands like the one above. Is there another function that would guarantee the wrapping of code? Thanks!

2

2 Answers 2

46

Use the width.cutoff argument inside tidy.opts knitr options to specify the output width :

```{r, tidy=TRUE, tidy.opts=list(width.cutoff=60)}
plot(DT$age, DT$height, xlab = "Age of participant in Trials", ylab = "Height of participant in trials")
```

You can define this option globally for your whole file with a chunk like this :

```{r}
library(knitr)
opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
```

The tidy.opts options are passed to the formatR package which does the tidying (if I understand correctly). In-depth informations about formatR can be found here :

http://yihui.name/formatR/

3
  • 20
    This doesn't work with long urls (let's say an string with no spaces) when exporting to PDF, they are just out of margin. Commented Sep 28, 2017 at 19:35
  • 10
    This is not working for me either. Could it be because the code in my chunks are bash, not R? ie. it looks like {bash, eval=F} and the commands run off the pdf page
    – rrr
    Commented Jun 15, 2018 at 16:38
  • 4
    This doesn't work for me in R, not standalone in each line, not with global options. Trying this right now and it's not working: {r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, tidy.opts=list(width.cutoff=60), tidy=TRUE) I have a very long line defining a matrix and it's not wrapped, despite has plenty of spaces inside. Commented Oct 18, 2020 at 19:26
15

The formatR solution also did not work for me, what worked for me was adding the below code to the YAML metadata

---
title: ...
author: ...
header-includes:
  \usepackage{fvextra}
  \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
---

In the .tex file, the Highlighting environment is used to print the code. The code above redefines the default Highlighting environment to include the breaklines option, which requires the fvextra package and creates the line wrap for us.

3
  • I just want to add for anyone reading this in the future that this suggestion worked for me, whereas the advice at the link intro2r.com/tips-tricks.html did not work for me. Now my R code wraps properly and does not run off the page. Thanks.
    – Novice
    Commented Jan 27, 2022 at 4:18
  • 1
    Would also just note that it actually needs to be "- \usepackage{fvextra}" and "- \Defineblahblah" in front of each line to make it work in the .Rmd file.
    – Isaac Zhao
    Commented Mar 21, 2022 at 0:17
  • This works successfully, very helpful. Commented Mar 25, 2023 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.