Incremental code evaluation in R markdown presentation - r

I'm looking for a way to evaluate code in a incremental way in R markdown presentation. I don't need to use any specific format - it can be anything that works and is flexible (rpres, ion, revealjs, etc.)
I'll use these presentation in class for my students and would like to make them type code in their R Console first and then compare our outputs.
So far I came up with one solution using revealjs with slide_level header
---
title: "Test presentation"
output: revealjs::revealjs_presentation
slide_level: 2
---
# Level 1 horizontal main slide
Some text
## Level 2 vertical slide with R Code
```{r eval=FALSE}
summary(cars)
```
## Level 2 vertical slide with R output
```{r echo=FALSE}
summary(cars)
```
Anything easier and less time consuming would be great.

I know the question is a bit old but you can use the incremental option for example with ioslides. That way you don't have to change slide but you can print step by step first the text then the code and then the result.
example code:
---
title: "Test presentation"
output:
ioslides_presentation:
incremental: yes
---
## Slide 1
- Some text
- Level 2 just the code
```{r eval=FALSE}
summary(cars)
```
- Level 2 just the output
```{r echo=FALSE}
summary(cars)
```

Related

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

R Markdown, numbering not showing for figure captions in HTML and Word

I am putting together an R Markdown document in HTML, and I have the following YAML:
---
title: "R Markdown Example"
author: "Me"
date: "October 30, 2017"
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
code_folding: "show"
---
I am trying to insert a figure with auto-numbered caption with the following code:
```{r, fig.cap="Figure caption \\label{fig_1}"}
plot(mtcars$hp, mtcars$mpg)
```
But the caption number won't show up. When I try to reference the image in the text
(Figure \ref{fig_1})
I just get:
(Figure )
I have the same problem. This only happens for Word and HTML outputs, so I think I may be using LaTeX documentation instead of the proper input for these types. I've tried a lot of the different recommendations for figure captions, but I can't seem to get any of it to work.
I needed to output to Word to adhere to a journal that only accepted Word... So I ended up using the captioner package. This should also work with html and pdf.
library(captioner)
fig_nums <- captioner()
In my r chunk I put my plot code and then the caption comes directly after the chunk in ticks, using the fig_nums function ``:
```{r}
plot(pressure)
```
`r fig_nums("pressure-plot", "pressure against temperature")`
It looks like this in the Word output:
Then I can refer to it in text like this:
As you can see from the nice visualisation in r fig_nums("pressure-plot", display = "cite") the pressure rises as
temperature rises.
This will show as:
As you can see from the nice visualisation in Figure 1 the pressure rises as
temperature rises
Be sure to run the first time you use, and check-out for more info:
install.packages("captioner")
vignette("using_captioner")
You can use the bookdown::html_document2 format.
---
title: "Untitled"
output: bookdown::html_document2
---
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
\#ref(fig:pressure)

Add special beamer slide using RMarkdown

I want to produce a beamer presentation from a RMarkdown file.
I am in this case using this beautiful template: https://github.com/matze/mtheme
How in markdown can you specify that you want to insert, not a "normal" slide, by a special one, such as an appendix slide, or a standout slide?
This template contains a standout slide that I would like to use as final slide as they do in their demo slides (http://mirrors.standaloneinstaller.com/ctan/macros/latex/contrib/beamer-contrib/themes/metropolis/demo/demo.pdf)
In beamer, I would do:
\begin{frame}[standout]
Questions?
\end{frame}
What is the equivalent in Markdown?
Setting frame options is pretty straight forward:
---
title: "Untitled"
output: beamer_presentation
theme: "metropolis"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## {.standout}
Questions?

Code in Columns in RMarkdown presentation

Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this

Rmarkdown beamer ignoring size option

I'm trying to create a presentation with Rmarkdown and beamer and I am unable to adjust the size of my code and output. In the past I edited the beamer template to work around this but I was hoping there is a better way.
For example:
---
title: "Reprex"
output:
beamer_presentation
---
## Output one
```{r, size="footnotesize"}
1:50
```
renders identical to:
---
title: "Reprex"
output:
beamer_presentation
---
## Output one
```{r}
1:50
```
Any thoughts on how to fix this?
The size option specifies the "font size for the default LaTeX output" (source). But you are writing rmarkdown which is converted to PDF afterwards.
A workaround:
---
output: beamer_presentation
---
## Output one
Foo.
```{r, echo = FALSE}
knitr::asis_output("\\footnotesize")
1:10 # this will be small
knitr::asis_output("\\normalsize")
11:20 # this not
```
Bar.
Depending on how often this is used it could be work using a helper function or even a chunk hook to print the font size commands.

Resources