Search code examples
rknitrr-markdownpandocbeamer

Removing blank space from code chunks in .RMD (compiling knitr to Beamer pdf)


I am creating slides on RStudio (Version 0.99.903) in an R Markdown .Rmd file, with output set to PDF (Beamer). On several slides, I would like to insert a plot, either below text or alone on a slide, with the code producing the plot not viewable on the screen. However, despite many things I've tried, there is always a space between the end of the text and the start of the figure, presumably where the code chunk would have appeared had I not set echo=FALSE. For example:

---
title: "Untitled"
author: "Math 35"
date: "October 14, 2016"
output: beamer_presentation
---

```{r setup, echo=FALSE, include=FALSE}
knitr::knit_hooks$set(mysize = function(before, options, envir) {
  if (before) 
    return(options$size)
})
knitr::opts_chunk$set(size='\\small')
knitr::opts_chunk$set(warning=FALSE)
knitr::opts_chunk$set(message=FALSE)
knitr::opts_chunk$set(fig.align='center')
```

## Recap from Last Time: Continuous Random Variables
-- Uniform Random Variable  $X\sim  U(a,b)$
$$f_X(x) = \frac{1}{b-a}, a \leq x \leq b$$
$$E[X] = \frac{a+b}{2}$$
$$Var(X) = \frac{(b-a)^2}{12}$$
```{r, echo=FALSE, fig.height=3, fig.width=3.5}
density <- dunif(x=seq(from=0, to=6, by=0.01), min=1, max=5)
plot(seq(from=0, to=6, by=0.01), density, col="black", type="l", ylim=c(0, 0.5), lwd=4,  xlab="X ~ U(1,5)")
  lines(c(3,3), c(0,dunif(3, min=1, max=5)), col="red", lwd=2)
  text(2.9, 0.1, "E[X]=(1+5)/2 = 3", col="purple")
```

When I "Knit PDF" within R Studio, the slide that is produced has a large blank space between the text and the figure. As a result, the figure doesn't fit on the slide. I would like to remove this blank space so that everything can fit on the slide.

Here are all the code chunk options I've tried that haven't worked:

  • results='hide' which would ordinarily hide regular command results but still show the figure, but in this case still leaves the blank space.

  • strip.white=TRUE

  • tidy=TRUE

  • fig.keep = 'high'

  • fig.keep = 'last'

  • highlight= 'false'

I've also looked at:

  • http://yihui.name/knitr/demo/output/ which refers to https://github.com/yihui/knitr/issues/231 but since that addresses the problem in a .Rnw file, which I'm unfamiliar with, I couldn't get that solution to work. I tried putting the suggested header at the top of the .Rmd file but "Knit PDF" didn't complete compiling. I probably did it wrong.

  • I've looked at the R Markdown reference guide which is where I found the various options I tried above.

I've spent now two hours trying to figure out how to get one figure to show up properly on one slide. Any help would be appreciated.


Solution

  • You could play with par(), but part of the problem comes from the latex side. Using a \begin{center}\end{center} environment adds vertical space. You could try to modify the relevant knitr hook, or simply add the centering instruction by hand,

    \centering
    ```{r, echo=FALSE, fig.height=1.5, fig.width=3.5}
    density <- dunif(x=seq(from=0, to=6, by=0.01), min=1, max=5)
    par(mar=c(2.5,2.5,0.5,0.5), mgp=c(1.5, 0.5, 0), bg="grey95")
    plot(seq(from=0, to=6, by=0.01), density, col="black", type="l", ylim=c(0, 0.5), lwd=4,  xlab="X ~ U(1,5)")
      lines(c(3,3), c(0,dunif(3, min=1, max=5)), col="red", lwd=2)
      text(2.9, 0.1, "E[X]=(1+5)/2 = 3", col="purple")
      ```
    

    enter image description here