Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Scrollbar appear in RMarkdown code chunks (html view)

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.

like image 490
rrr Avatar asked Jul 25 '16 22:07

rrr


People also ask

How do I show code chunks in R markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS). There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options.

How do you show results in RMarkdown?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

What is knitr RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


1 Answers

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;
}
```
like image 120
hrbrmstr Avatar answered Oct 17 '22 04:10

hrbrmstr