45

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!

user1398057
  • 1,109
  • 1
  • 11
  • 15
  • Not a great solution, but try [this](http://tex.stackexchange.com/questions/41471/getting-sweave-code-chunks-to-stay-inside-page-margins). – jlhoward Oct 06 '14 at 06:17
  • Possible duplicate of [knitr: How to prevent text wrapping in output?](http://stackoverflow.com/questions/12176296/knitr-how-to-prevent-text-wrapping-in-output) – Jim G. Sep 17 '16 at 05:16

2 Answers2

45

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/

Jim G.
  • 15,141
  • 22
  • 103
  • 166
juba
  • 47,631
  • 14
  • 113
  • 118
  • 18
    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. – Pablo Casas Sep 28 '17 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 Jun 15 '18 at 16:38
  • 3
    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. – Fran Marzoa Oct 18 '20 at 19:26
7

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.

jng
  • 79
  • 2
  • 4
  • I just want to add for anyone reading this in the future that this suggestion worked for me, whereas the advice at the link https://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 Jan 27 '22 at 04:18
  • 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 Mar 21 '22 at 00:17
  • This works successfully, very helpful. – EngineerDanny Mar 25 '23 at 02:05