Graphics on next slide with ioslides using Rstudio's Rmd - r

I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.
---
output: ioslides_presentation
---
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Any one have any idea on how to do this in Rstudio?
I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.

It's easier to run the code twice, once not evaluating the code, the second time not showing the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot 1
```{r, eval = FALSE}
plot(1:10)
```
## Plot 2
```{r, echo = FALSE}
plot(1:10)
```
It also avoids the error when smaller is true.

To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:
```{r setup, include=FALSE}
library(knitr)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
paste0('\n\n----\n\n', hook_plot(x, options))
})
})
```

If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot code
```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```
## Plot display
```{r cars_plot, echo = FALSE, eval = TRUE}
```
```

I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.
You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.

Related

RMarkdown suppress automatic figure numbering to html

Is there a way to suppress automatic figure numbering made by rmarkdown? The first plot rendered by R is the second figure, it added a Figure 1: prefix which is not what I need. I see a solution for PDF output but not html output.
Best,
Shixiang
It's difficult to know what you are looking for without a minimal reproducible example. But maybe you are looking for bookdown::html_document2 output.
Try the following:
---
title: "Example Document"
author: "Your name here"
output:
bookdown::html_document2
---
```{r cars, fig.cap = "plot"} # Figure with caption
plot(cars)
```
```{r cars3, fig.cap = ""} # Figure without caption
plot(cars)
```
This is the output of the above:

How can I automate different spacing between text and code blocks in R Markdown?

Consider the following R Markdown document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text.
I want all the body text to be double-spaced,
but I want all echoed code from code chunks to be single spaced.
In other words, not this:
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
Is there a canned or relatively painless way to have all normal text double-spaced, but have all code echoed from code chunks single spaced? I tried consulting the guide to chunk options here, but couldn't quite find what I was looking for.
If you are outputting to pdf the most painless way might be adding some LaTeX commands to your Rmd document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text. I want all the body text to be double-spaced, but I
want all echoed code from code chunks to be single spaced. In other words, not
this:
\singlespacing
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
\doublespacing
Some additional body text. Nor hence hoped her after other known defer his.
For county now sister engage had season better had waited. Occasional mrs
interested far expression acceptance. Day either mrs talent pulled men
rather regret admire but. Life ye sake it shed. Five lady he cold in meet up.
Alternatively, you could define a new chunk option using knitr chunk hooks. For instance, you could include in the setup chunk:
```{r setup, include=FALSE}
hook_chunk = knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(chunk = function(x, options) {
regular_output = hook_chunk(x, options)
# add latex commands if chunk option singlespacing is TRUE
if (isTRUE(options$singlespacing))
sprintf("\\singlespacing\n %s \n\\doublespacing", regular_output)
else
regular_output
})
knitr::opts_chunk$set(echo = TRUE, singlespacing = TRUE)
```
Some useful references:
Hooks - Customizable functions to run before/after a code chunk, tweak the output, and manipulate chunk options
How to Create New Chunk Options in R Markdown

Multiple plots in a single row

From R for Data Science:
To put multiple plots in a single row I set the out.width to 50% for
two plots, 33% for 3 plots, or 25% to 4 plots, and set fig.align =
"default". Depending on what I’m trying to illustrate ( e.g. show data
or show plot variations), I’ll also tweak fig.width, as discussed
below.
How do I put multiple plots on a single row, using the method described above? I could use a package such as patchwork, but the purpose of this post is to understand what's being described above. The R Markdown below doesn't generate what I'd expect, two 'pressure' plots on the same row.
---
title: "Untitled"
author: "April 2018"
date: "4/11/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Text
```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
plot(pressure)
```
```{r pressure2, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(pressure)
```
I’ve got no idea why your code (or rather, the code from R for Data Science) is not working. But plotting different data makes it work for me:
```{r pressure1, echo=FALSE, fig.width=6, fig.asp=0.618, out.width="50%", fig.align="default"}
plot(cars)
plot(pressure)
```
Alternatively, it seems to be enough to just specify different parameters, e.g.:
plot(pressure)
plot(pressure, main = '')
The fact that inconsequential changes fix the output indicates to me that this is a bug in RMarkdown.
That said, the easiest, most controlled way is to put par(mfrow = c(1, 2)) into the hunk directly before the plotting commands (or a ggplot2 solution such as faceting or {patchwork}).
Tweaking the alignment via the RMarkdown options can be tricky, due to spacing inserted by the conversion to HTML.
You need to specify fig.show = "hold" along with out.width, as you can see here: https://bookdown.org/yihui/rmarkdown-cookbook/figures-side.html

Adding space around figures in RMarkdown

I would like to add space around figures in RMarkdown. I am knitting to PDF and really don't like how close figures (or also equations) are to the text or to the next figure.
---
output: pdf_document
---
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r pressure2, echo=FALSE}
plot(pressure)
```
There is just too little space between the two plots and this gets more fuzzy when using ggplots.
Right now I use the Latex solution
\vspace{10pt}
but it would be nice if I could make a setting globally for the entire document.
Concerning the spacing before and after plots you can use a simple knitr hook:
```{r, echo = F}
library(knitr)
if(is_latex_output()) {
plot_default <- knit_hooks$get("plot")
knit_hooks$set(plot = function(x, options) {
x <- c(plot_default(x, options), "\\vspace{25pt}")
})
}
```
Here we alter the plot hook in that sense that we just add a spacing of 25pt after each plot output.
Concerning equations you can just add these four length definitions at the beginning of your document:
\setlength{\abovedisplayskip}{25pt}
\setlength{\belowdisplayskip}{25pt}
\setlength{\abovedisplayshortskip}{25pt}
\setlength{\belowdisplayshortskip}{25pt}
The first two alter equations created using the align environment. The latter two those created using $$ ... $$.

R markdown Page Breaks from Function

I am trying to break to a new page from a R markdown document that is calling a function to produce a set of graphics for each set of data (400+ pages in some cases). I am using LaTeX.
Any ideas? Here is a simple example that demonstrates some my challenge though it doesn't use a function to produce the output.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
summary(cars)
cat("\\pagebreak")
print(cars)
```
Update #1:
My example didn't demostrate the use of a function within the code chunk. Here is a better example.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
pr_w_pagebreak <- function() {
print(summary(cars))
cat("\\pagebreak")
print(cars)
}
pr_w_pagebreak()
```
The trouble you'll have with your chunks is that you want some results to appear as 'markup' and some to appear 'asis'. If you want to get the page break with the single block as you have it, you will need to do:
```{r, echo=-2}
summary(cars)
knitr::asis_output("\\pagebreak")
print(cars)
```
Otherwise, you'll have to do
```{r}
summary(cars)
```
\pagebreak
```{r}
print(cars)
```
And as Floo0 points out, you can get LaTeX code from the R chunks if you use results = 'asis', but you have to be careful because the following will not give you what you want.
```{r, results='asis'}
summary(cars)
cat("\\pagebreak")
print(cars)
````

Resources