Missing horizontal scroll bar in R Markdown HTML code chunks and output

This is basically @JeffKeller's answer but instead of having a separate CSS file and fiddling around in the YAML header you can just write your CSS inside your .Rmd file as a code snippet! For example,

```{css, echo=FALSE}
pre, code {white-space:pre !important; overflow-x:auto}
```

I tend to do this at about the same early point where I do other bits of setup e.g.

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

(Perhaps this is more of a comment but I can't really format it as such. Judging from the comments on the other answer though, I think some people might find this useful.)


With some help from folks in the comments, I was able to piece together a solution. There are two steps:

  1. Include a custom style sheet in the .Rmd YAML header:

    css: report_styles.css

    Which contains these styles:

    pre, code {white-space:pre !important; overflow-x:scroll !important}

    This makes it so that echoed code chunks will not wrap and that they have a horizontal scroll bar. It will also make is so that chunk outputs will not wrap further when the browser window is resized.

  2. Now, to get the chunk output to not wrap initially, we need to set options(width=a-big-number) as per this question.