Souping up R Markdown!

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).

  1. Using knitr::include_graphics for all figures
  2. Using the here::here() package & changing figure sizes
  3. Adding logos/images in your title or header/footer
  4. Show the R code chunks verbatim
  5. Including variables in R chunks from other R chunks!
  6. Icons and Emojis
  7. Interactive Plots
  8. Sourcing other scripts to generate content
  9. Making columns in Rmarkdown
  10. 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 (![](path/to/figure)), 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')