R Knitr Markown: Setting HTML Page Width

R knitr markown: Setting HTML page width

Add a css element to your document, e.g.

---
title: "test"

output:
html_document:
css: "test.css"
---

and then put your new css information in that file, e.g.

body {
max-width: 1100px;
margin: auto;
padding: 1em;
line-height: 20px ;
}

Adjust the output width of R Markdown HTML output

Add this at the start of your document:

```{r set-options, echo=FALSE, cache=FALSE}
options(width = SOME-REALLY-BIG-VALUE)
```

Obviously, replace SOME-REALLY-BIG-VALUE with a number. But do you really want to do all that horizontal scrolling?

Your output is probably being wrapped somewhere around 80 characters or so.

Set page width in Knitr for md or HTML output

knitr does not take care of markdown rendering directly, and you can compile *.md to *.html through the markdown package, for which knitr has a wrapper function knit2html(). To get the table of contents, you can add the toc option to markdown::markdownToHTML(), e.g.

library(knitr)
knit2html('foo.Rmd', options = c('toc', markdown::markdownHTMLOptions(TRUE)))

Override rmarkdown theme in order to change html page width

Create a file styles.css with your styles:

<style>
.main-container {
width: 100%;
max-width: unset;
}
</style>

And include it in your rmarkdown as described here:

---
output:
html_document:
css: styles.css
---

Both options work for me.

Changing the maximum width of R markdown documents

There's no way to change that number specifically, but you can override it. Create your own style.css file in the same directory as your document, and give it some content:

body .main-container {
max-width: 500px;
}

Then reference that CSS file in your YAML front matter:

---
...
output:
html_document:
css: style.css
---

reescale html text when the window size is changed

We can use custom css styles to achieve scaling:

---
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Custom CSS
```{css, echo=FALSE}
h1 {
font-size: 5.9vw;
}
h2 {
font-size: 3.0vh;
}
p {
font-size: 2vmin;
}
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Look at https://bookdown.org/yihui/rmarkdown-cookbook/html-css.html and https://css-tricks.com/viewport-sized-typography/ for more details concerning inclusion of CSS in RMd and Viewport based scaling in CSS.

Big Window

Small Window

If we want to fix the width of the output to a page size we can do so by fixing the width of the main container. Then we should be able to use manual linebreaks at the positions we need.

---
title: "STARTSTARTSTART         ENDENDEND"
date: "`r format(Sys.time(), '%d de %B, %Y')`"
output:
html_document
---
<!--- Change width options to apppropiate values for your desired page size-->
<style type="text/css">
.main-container {
width: 20cm;
max-width: 20cm;
min-width: 20cm;
margin-left: auto;
margin-right: auto;
}
</style>

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

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

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

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
```

Sample Image


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>

Sample Image



Related Topics



Leave a reply



Submit