David
David

Reputation: 10192

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.

Upvotes: 4

Views: 1705

Answers (1)

David
David

Reputation: 10192

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

result

Upvotes: 5

Related Questions