9

I am making an RMarkdown document using RStudio and knitr. I want my code chunks to print without wrapping text on the html file I create. Is there an option I am missing that stops text wrapping of code? So far I have only found questions about how to remove scrollbars, making me think that maybe something has changed recently. (RStudio Version 0.99.892, R Version 3.2.2) Thanks!

Simple example RMarkdown document. (The setup section is the default):

---
title: "Stop looking bad RMarkdown!"
output: html_document
---

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

#### I want this to print without text wrapping:  

```{r}
x <- matrix(nrow = 3, ncol = 20, data = 1)
x
```

If you run that you will see that the matrix x is split into 2 lines. I want it to just be one line that you have to scroll along to see the whole thing.

rrr
  • 1,914
  • 2
  • 21
  • 24

1 Answers1

16

try:

---
title: "Stop looking bad RMarkdown!"
output: html_document
---

<style>
pre code, pre, code {
  white-space: pre !important;
  overflow-x: scroll !important;
  word-break: keep-all !important;
  word-wrap: initial !important;
}
</style>

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

#### I want this to print without text wrapping:

```{r }
x <- matrix(nrow = 3, ncol = 20, data = 1)
x
```

NOTE that with more recent versions of R markdown you can replace the <style> tags with:

```{css}
pre code, pre, code {
  white-space: pre !important;
  overflow-x: scroll !important;
  word-break: keep-all !important;
  word-wrap: initial !important;
}
```
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • What does the ` – rrr Jul 26 '16 at 21:14
  • the `html_document` has a default style sheet that sets different parameter for `pre code` and `pre` and `code` tags. so we have to override them. the R `options` is needed to ensure proper line breaks will be in place to enable the ` – hrbrmstr Jul 27 '16 at 02:08
  • This was working great, and then (perhaps with an update) the css code chunk started appearing in the markdown document. Hide it by adding into the curly brackets: `{css, echo=FALSE}`. The scrollbar will still be there, but the css code chunk will not be visible. – rrr Mar 11 '18 at 20:25
  • Is it possible to get a plot in scrollbox? – ktyagi Sep 21 '18 at 15:14