Skip to content

Instantly share code, notes, and snippets.

@tomhopper
Last active January 16, 2019 23:53
Show Gist options
  • Select an option

  • Save tomhopper/d85d7a34ca4e674a563c485676a5d23d to your computer and use it in GitHub Desktop.

Select an option

Save tomhopper/d85d7a34ca4e674a563c485676a5d23d to your computer and use it in GitHub Desktop.

Revisions

  1. tomhopper revised this gist Jan 16, 2019. 1 changed file with 16 additions and 12 deletions.
    28 changes: 16 additions & 12 deletions knitr_Wide_Output.Rmd
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,19 @@
    <style>
    pre {
    overflow-x: auto;
    }
    pre code {
    word-wrap: normal;
    white-space: pre;
    }
    </style>

    First-thing at the top of the Rmd file, after the YAML header, is the above
    css style block. Alternatively, this could be included in a css file that is
    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:

    ---
  2. tomhopper created this gist Jan 16, 2019.
    44 changes: 44 additions & 0 deletions knitr_Wide_Output.Rmd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <style>
    pre {
    overflow-x: auto;
    }
    pre code {
    word-wrap: normal;
    white-space: pre;
    }
    </style>

    First-thing at the top of the Rmd file, after the YAML header, is the above
    css style block. Alternatively, this 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)
    ```