Search code examples
rr-markdownslidy

slide dimensions in r slidy


Using the default RStudio slidy html presentation template creates slides that have figures with fixed size (I believe 7 inches x 5 inches). When viewing on a larger/higher resolution screen, that can leave a lot of empty space (zooming only changes the size of the text, not the figure):

enter image description here

But adding knitr::opts_chunk$set(echo = FALSE, out.width = '100%') to an R chunk in order to auto-scale the figure size results in a figure that's too tall and requires scrolling to see all the content:

enter image description here

Using out.height='100%' instead of out.width='100%' seems to revert to the original size without changing the figure dimensions:

enter image description here

Is there a better way to control slide dimensions and/or the size of content on html slides other than manually specifying the size of content in inches? When I look at example slides online, they all fill the screen very nicely (e.g.). How does one go about actually accomplishing that? Or is the "solution" to just use PDF output?

For reference, here is the default slidy presentation code:

---
title: "Untitled"
author: "Author"
date: "`r Sys.Date()`"
output: slidy_presentation
---

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

## R Markdown

This is an R Markdown presentation. 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.

## Slide with Bullets

- Bullet 1
- Bullet 2
- Bullet 3

## Slide with R Output

```{r cars, echo = TRUE}
summary(cars)
```

## Slide with Plot

```{r pressure}
plot(pressure)
```

Solution

  • You can define a knitr hook to enclose the figure in a div tag with class myfigure:

    ```{r setup, include=FALSE}
    knitr::knit_hooks$set(myfigure = function(before, options, envir) {
      if(before) {
        "<div class='myfigure'>"
      } else {
        "</div>"
      }
    })
    ```
    

    Then, using CSS, define the height of the elements having the myfigure class in the vh unit (percentage of the viewport):

    ```{css}
    .myfigure { 
      height: 75vh;
    }
    ```
    

    Now use this stuff as follows:

    ```{r pressure, echo=FALSE, out.width="100%", out.height="100%", myfigure=TRUE}
    plot(pressure)
    ```
    

    Better, instead of using the same height for all elements having the myfigure class, you can define this hook which allows to set the height in the chunk options:

    ```{r setup, include=FALSE}
    knitr::knit_hooks$set(myfigure = function(before, options, envir) {
      if(before) {
        sprintf("<div class='myfigure' style='height:%svh'>", options$myheight)
      } else {
        "</div>"
      }
    })
    ```
    

    And use this hook as follows (remove the above CSS chunk):

    ```{r pressure, echo=FALSE, out.width="100%", out.height="100%", myfigure=TRUE, myheight="75"}
    plot(pressure)
    ```