Skip to content

Instantly share code, notes, and snippets.

@tomhopper
Last active January 16, 2019 23:53
Show Gist options
  • Save tomhopper/d85d7a34ca4e674a563c485676a5d23d to your computer and use it in GitHub Desktop.
Save tomhopper/d85d7a34ca4e674a563c485676a5d23d to your computer and use it in GitHub Desktop.
Create a code chunk in an RMarkdown document that is wider than the page and scrolls horizontally.
First-thing at the top of the Rmd file, after the YAML header, is the
following css style block.
<style>
pre {
overflow-x: auto;
}
pre code {
word-wrap: normal;
white-space: pre;
}
</style>
(Thanks to [this answer](https://stackoverflow.com/a/36846864/393354) by rawr on Stack Overflow.)
Alternatively, this css could be included in a css file that is
included in the document via the YAML headers, like so:
---
title: "Document Title"
author: "John Author"
date: '`r format(Sys.Date(), "%B %Y")`'
output:
html_document:
css: custom_css.css
---
When we want to have a code chunk that scrolls, we use code blocks to
save the current output width for R, then set a wide output width.
```{r, include=FALSE}
set_width <- getOption("width")
options(width = 999)
```
Then we produce our wide output, being sure to set the `tidy=FALSE`
chunk option.
```{r, tidy=FALSE}
mtcars %>% skim()
```
Finally, we set the output width back to the previous value so that
future code blocks will produce the normally-expected formatting.
```{r, include=FALSE}
options(width = set_width)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment