Souping up R Markdown!
2020-02-20
For folks that are just getting started in R, or for those who have so far avoided doing anything that isn’t just coding in R, I wanted to jot down a few tips that I’ve found useful when working with the magic that is Rmarkdown!
This is not meant to be a comprehensive tutorial, and it may not even be useful to you, but it may highlight a few new things you can try, or inspire you to at least jump into the world of Rmarkdown. It’s really transformed the way I do analysis, communicate, create websites, write, etc. For actual details on souping up RMarkdown, see http://rmarkdown.rstudio.com, or any of the other great resources that actual experts have written.
But for now let’s soup it up!
10 Tips to Soup things Up
Here’s an internally linked list to the sections! To link, you can add a {#custom_label_here} at the end of the section title. Something like ## My Section {#custom_label_here} and then reference as [a section name](#custom_label_here).
- Using
knitr::include_graphicsfor all figures- Using the
here::here()package & changing figure sizes- Adding logos/images in your title or header/footer
- Show the R code chunks verbatim
- Including variables in R chunks from other R chunks!
- Icons and Emojis
- Interactive Plots
- Sourcing other scripts to generate content
- Making columns in Rmarkdown
- Maps Maps Maps!
1. Using knitr::include_graphics and here::here()
This one is something I use pretty much always. It has made things soo much easier in many ways. It’s also very flexible. By calling a graphic (could be a figure, picture, plot, whatever) inside an R-chunk with knitr::include_graphics() instead of using the markdown syntax (), you can control the size, placement, etc etc. Even better, when you are using RStudio projects, you can use relative pathnames to a file on your computer instead of having to deal with the pain of using the .. to signify going “up” a directory outside of the place the .Rmd lives. Zev Ross has a great post showing how to resize and customize images and figures in Rmarkdown…check it out here.
For example, let’s look at this picture of a can of soup, using a weblink.
From a url
# straight from web:
knitr::include_graphics('https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Can%2C_food_%28AM_2007.41.2-1%29.jpg/766px-Can%2C_food_%28AM_2007.41.2-1%29.jpg')a beatiful can of soup from a web URL
2. Using here::here()
We’re going to do the same thing as above, but we’ll use a path to a file on my computer. The trick here is Rmd has the helpful (?) habit of defaulting to the directory it lives in…so anything not in the same folder as your .Rmd file requires some extra redirection before you click Knit. Otherwise the computer can’t find whatever it is you want plotted/printed.
Enter the great package {here}. Please go look just to see Allison Horst’s awesome art. Anyway, we can use the here::here() syntax to keep the path relative and avoid things breaking, or having to change all the setwd() type links.
Here we can also tweak the size of our photo and placement inside the header of the R chunk. Here’ I’m adding these options to my code chunk:
```{r knitrsoupTst, fig.cap= "a beatiful can of soup from a local file", echo=TRUE, eval=TRUE, out.width='25%', fig.align='left'}
```
Note, the out.width='25%' and fig.align='left' options here. They allow us to change the size of our figure as well as the location.
# download.file(url="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Can%2C_food_%28AM_2007.41.2-1%29.jpg/766px-Can%2C_food_%28AM_2007.41.2-1%29.jpg", destfile = "img/toheroa_soup.jpg")
# now wrap with paste0()
knitr::include_graphics(paste0(here::here(), "/img/toheroa_soup.jpg"))a beatiful can of soup from a local file
3. Add a Logo in your title/header/footer
This is just one demonstration, but there are two parts to this. First, you can include icons in your header or footer! Depending on whether you are outputting an html or pdf file this approach may change slightly, but the basics are here.
For PDF You can include the icon/emblem whatever in your .yaml header. This may look like this:
title: |
{width=0.25in}
Souping up R Markdown!
date: "2020-02-20"
output:
html_document:
code_folding: hide
Important : To keep your icon and title on different lines, you’ll want to add 2 spaces at the end of the line with the photo/image. See here for more details: https://bookdown.org/yihui/rmarkdown-cookbook/latex-logo.html
For HTML
This is a bit different. Here we can add a logo by including an R chunk with a path to the logo. We can then specify we want it to live at the top in the header using style = 'position:absolute; top:0; right:0; padding:10px; width:100px. Play with the size with the width: option. For a footer, we can do a similar option but change to: style = 'position:absolute; top:0; right:0; padding:10px; width:100px
htmltools::img(src = knitr::image_uri(file.path(paste0(here::here(), "/img/toheroa_soup.jpg"))),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:10px; width:100px')