3

Writing the following R-Markdown chunk at the beginning of an R-Markdown file allows me to wrap a line in R-Markdown PDF output, provided that the R package formatR is installed and the line to wrap has a whitespace character. How do I wrap a line if the line does not have a whitespace character?

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

For example, the following R chunk demonstrates failure to wrap and wrapping after 27 characters.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

enter image description here

I would like the first command to render as

# devtools::install_github(
"tslever/Tom_Levers_Git_Rep
ository/TomLeversRPackage")

Possibly another question: With the above options, the following R chunk is rendered as one line. How do I prevent this reformatting, and still wrap after 27 characters?

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
test.data.frame <- read.csv(
    file = "C:/Users/Tom/Documents/Tom_Levers_Git_Repository/test.csv"
)
```

enter image description here

Tom Lever
  • 321
  • 3
  • 16
  • 1
    Can you clarify on how you plan on wrapping text and what your expected output would look like? Is there a function that returns a long string (e.g. something like `paste0(sample(c("A", "C", "G", "T"), 1000, replace = TRUE), collapse = "")`) and you want to print this by wrapping the string every 30 characters? Or are you after something else? – Maurits Evers Sep 02 '22 at 05:30
  • Thanks for asking for clarity. Pleas see updated post. – Tom Lever Sep 02 '22 at 06:23

2 Answers2

3

HTML output

You can use some custom CSS; here is a minimal RMarkdown example:

---
title: "Untitled"
output: html_document
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{css, echo=FALSE}
pre {
    width: 30ch;
}
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

This limits the width of code chunks to 30 characters (width: 30ch) and produces

enter image description here

PDF output

---
title: "Untitled"
output: 
    pdf_document:
        pandoc_args: --listings
        includes:
            in_header: preamble.tex
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
```

with the following preamble.tex

\lstset{
    breaklines=true,
    basicstyle=\ttfamily,
    linewidth=30ex
}

This produces

enter image description here

Note that this probably requires some tuning/tweaking of listings parameters. The key here is that you need breaklines=true to enable breaking long lines, and then define a line width with e.g. linewidth=30ex. Note that this won't exactly give you 30 characters, as this is not how widths are specified in LaTeX; so you'll need to adjust the width to suit your aesthetic needs.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • How do we make this work with PDF output? Is the number of characters in most lines of the R chunk actually less than 30? – Tom Lever Sep 02 '22 at 06:53
  • @TomLever Ah I missed the "PDF output" part from your post. Yes, this is for HTML only, and yes, the width of the code chunk is exactly 30 characters long. Need to think about PDF output... – Maurits Evers Sep 02 '22 at 06:58
  • Which are the 30 characters in the first line of output? Even if there should be 27, why is the first "(" on the second line when this "(" is the twenty-seventh character of the command, counting from 1? – Tom Lever Sep 02 '22 at 07:01
  • Not 30 characters inside the code chunk. The width of the code chunk is 30 characters. This is a number that probably requires some manual tweaking. Please be aware (and prepared) that this is in any case an ugly solution as this hard-wraps lines of code (ignoring whitespaces, words etc.). – Maurits Evers Sep 02 '22 at 07:06
  • @TomLever I've added a PDF solution; please take a look. – Maurits Evers Sep 02 '22 at 07:14
  • Please see below. How do I split at either space (while keeping the space) or at end of line, like in Microsoft Word? Additionally, how do I color the output chunk white? – Tom Lever Sep 02 '22 at 08:17
0

Based on Maurits Evers's work,

preamble.tex

\lstset{
    backgroundcolor = \color{lightgray},
    breaklines=true,
    breakindent=0ex,
    basicstyle=\ttfamily,
    linewidth=109ex
}

and Test.Rmd

---
title: "Test"
output: 
    pdf_document:
        pandoc_args: --listings
        includes:
            in_header: preamble.tex
date: "2022-09-02"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

```{r, eval=TRUE, echo=TRUE, results="show", message=FALSE}
# devtools::install_github("tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
# devtools::install_github( "tslever/Tom_Levers_Git_Repository/TomLeversRPackage")
"<string of a's as long as the immediately above command that I cant enter in SO>"
```

yielded

enter image description here

Tom Lever
  • 321
  • 3
  • 16