xaringan slides
By R package build in Personal
July 14, 2021
For my entire academic career I have been using Beamer slides in LaTeX for all my conference presentations and lecture slides. While Beamer is great for handling things like mathematical equations, table of contents, creating pauses, …, it is not so great for incorporating R content. For instance to create the following slide:
I had to:
- Create a LaTeX (
.tex
) file of class beamer - Open R and run the following code:
x <- rnorm(100)
hist(x)
- Save the outputted plot as an image
- Copy and paste the source code into a verbatim environment in LaTex document
- Refer to my saved plot by file path in the LaTeX document
- Compile the LaTeX document.
While this is tedious enough, image having to redo this if, for example, I instead wanted to use a standard deviation of 2. Rather than simply adjusting my code and recompiling a single document, I have to redo steps 2–6. If this wasn’t bad enough, the LaTex syntax is not the easiest on the eyes:
\begin{frame}[fragile]
\begin{Verbatim}
> x <- rnorm(100)
> hist(x)
\end{Verbatim}
\begin{figure}[htbp]
\begin{center}
\includegraphics[width= 0.7\textwidth]{histogram}
\caption{A histogram of 100 random numbers generated from a normal
(i.e Gaussian) distribution with mean 0 and standard deviation of 1.}
\label{fig:hist}
\end{center}
\end{figure}
\end{frame}
I thought I found a solution by using knitr or Sweave to create Beamer slides directly with Rstudio (you can read more about how to do this here). This solution allowed me to bypass the need to save any output files produced by R (since R studio and Sweave did that for me) with the added benefit of only having a single file to edit and compile. In addition, it had some great features which included automatic colour coding of R syntax (for example, functions appear in red, while arguments appear in green in the example slide provided below) along with the ability to handle code chunk options (I discuss some features and benefits in a previous blog post). While I used this solution in my Data 101 course, I found the long compile time to be big hindrance in my productivity. Furthermore, the error messages produced in R studio were often cryptic which sometimes resulted in me opening and compiling the document with LaTeX in order to pinpoint the source of the error.
While never fully satisfied with any of these options, I finally came across a game changing solution. While Googling “How to make a website using R” I came across this YouTube video. It was co-created by Alison Hill who I have referenced and praised in previous posts for her incredible content. At around 38:36 Alison mentions how she uses ?“shar engine”/“sharing again “? slides to make her presentations. Given the unusual name and inaudible caption produced by YouTube it took me a few failed google attempts to discover that she was using Xaringan to create her slides: an R package developed by Yihui Xie (another motivational R genius who I have been geeking out over). This rabbit hole led me down another tangent of tutorials/blogs/YouTube videos/web books on how I could produce these in R. Rather than saying it in a blog post I have summarized some of what I have learned within a, you guessed it, xaringan presentation slide deck! You can either point to the slides using a simple link: simple link, which was produced using
[simple link](/slides/NHSRxaringan.html)
or embed the slide deck using
knitr::include_url('https://vrbiki.github.io/learning-xaringan/NHSRxaringan.html')
Notice that this method requires your slides to have a URL. The slide deck talks about how you can do that using GitHub (the source file for producing those slides are also available here. Alternatively I could have used the URL: https://irene.vrbik.ok.ubc.ca/slides/NHSRxaringan.html#1
test for lecture slide hellloooo
Resources
- Sharing Your Work with xaringan workshop: https://spcanelon.github.io/xaringan-basics-and-beyond/index.html
- R Markdown: The Definitive Guide (Ch 7) https://bookdown.org/yihui/rmarkdown/xaringan.html
- Posted on:
- July 14, 2021
- Length:
- 3 minute read, 628 words
- Categories:
- Personal
- See Also:
- Blogdown Cheatsheet