Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Width of R code chunk output in RMarkdown files knitr-ed to html

Question:

What is the current working solution to set the width of r code output in html files? I would like to set width to something big and use a slider in the html output.

options(width = XXX) seems not to work anymore.

Example:

---
title: "Width test"
output:
  html_document:
    theme: default
---
```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 15)
dataM
```

Result:

enter image description here

sessionInfo() output on the screenshot above.

Related:

(options(width = 999) is not working for me)

knitr: How to prevent text wrapping in output?

How to adjust the output width of RStudio Markdown output (to HTML)

like image 305
m-dz Avatar asked Apr 25 '16 15:04

m-dz


People also ask

How do I change the output size in R Markdown?

To change the output size you can use the corresponding LaTeX commands, set just before the code junk. The smallest option would be \tiny . For a full overview consider e.g. this. After the code junk it's important to set back to the size you used before, e.g. \normalsize .

How do I save a R Markdown file as HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I share R Markdown in HTML?

By default, RMarkdown rendered as HTML are self-contained. This means you can compile the document into HTML file and simply email it to a colleague. It should have all that is needed to render the document on your colleague's web browser.

When using R Markdown and knitr How do you indicate the height and width of a plot created in a code chunk?

When using knitr, how do you denote the height an width of a plot created in a code chunk ? Answer : Set the 'fig. height' and 'fig. width' options for the code chunk.


1 Answers

You can use this to make the pre blocks scroll horizontally if it overflows.

---
title: "Width test"
output:
  html_document:
    theme: default
---

<style>
pre {
  overflow-x: auto;
}
pre code {
  word-wrap: normal;
  white-space: pre;
}
</style>

```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 20)
dataM
```

enter image description here


For a scrollable height, create a container div with a max height and a overflow-y: auto; or overflow-y: scroll;

Similar question/answer

---
title: "Height test"
output:
  html_document:
    theme: default
---

<style>
.pre-scrolly {
  max-height: 150px;
  overflow-y: auto;
}
</style>

<div class='pre-scrolly'>
```{r}
sessionInfo()
```
</div>

enter image description here

like image 104
rawr Avatar answered Sep 29 '22 12:09

rawr