# Adding background color to specific slide in beamer when using rmarkdown

I want to highlight an entire slide in a presentation I am creating in R markdown, much in the same way as was done in this answer (without using markdown).

Cannot seem to figure it out though. Below is a dummy example of my failure so far...

---
title: "Untitled"
output: beamer_presentation
---

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


## Slide with Bullets

- Bullet 1
- Bullet 2
- Bullet 3

% \setbeamercolor{background canvas}{bg=red}

## Slide with R Output

\setbeamercolor{background canvas}{bg=red}

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


• Does colortheme: "dolphin" work? (Reference: this website.) Aug 14 '16 at 12:41
• @heather only to change the whole set of slides, not a specific slide Aug 14 '16 at 13:57
• Okay, sorry, I'll keep looking. Aug 14 '16 at 13:58
• throws a pandoc error. Aug 14 '16 at 14:11
• yeah, I made a mistake with that last one, that's why I deleted it (that was normal LaTeX, not R markdown...sorry). I'm still looking Aug 14 '16 at 14:12

I created a key, bgcolor, that can be passed as an option to a frame. In the example below, only the second slide has a red background.

\documentclass{beamer}

\makeatletter
\define@key{beamerframe}{bgcolor}[]{%
\ifx#1\@empty\else
\setbeamercolor{background canvas}{bg=#1}%  background color
\fi}
\makeatother

\begin{document}

\begin{frame}
Test
\end{frame}

\begin{frame}[bgcolor=red]
Test
\end{frame}

\begin{frame}
Test
\end{frame}

\end{document}


As a workaround, you could hardcode for which frame number the background colour should be changed. This can be done in the preamble.

\documentclass{beamer}

\setbeamertemplate{background}{%
\color{red}\rule{\paperwidth}{\paperheight}
\fi
}

\begin{document}

\begin{frame}
abc
\end{frame}

\begin{frame}
abc
\end{frame}

\begin{frame}
abc
\end{frame}

\end{document}