I write in rmarkdown (check out that link, it’s to the new Rmarkdown book!) most days, and I’ve been using it for the past few years.
I’ve picked up a few tips along the way, and I wanted to share a couple of things I think give you a nice return on investment.
Tip number 1: Save the images that you create
You want to make sure that you save images you create in your rmarkdown document. To do this automatically, and avoid writing things like
dev.off(), just create the plots in rmarkdown, and tell it you want to save the output.
In the funky looking code at the top, the YAML, specify
--- title: "Amazing document" output: html_document: keep_md: yes ---
What happens now?
Well, when you run your rmarkdown document, you get a new folder WITH THE FIGURES
- insert amazing gif -
Tip Number 2: Reduce clutter and increase sanity: Set options in a code chunk.
Want to change the width of your figure? Want to save to PNG, JPG, or more?
In your rmarkdown code chunk, this thing
You can specify these things!
Want to save a figure as JPG?
```r dev = "jpg" ggplot(airquality, aes(x = Temp, y = Wind)) + geom_point() ```
What about a PNG?
```r dev = "png" ggplot(airquality, aes(x = Temp, y = Wind)) + geom_point() ```
Why not both?
```r dev = c("png", "jpg") ggplot(airquality, aes(x = Temp, y = Wind)) + geom_point() ```
This is really handy, say, when a journal wants all your figures as TIFF or PS.
```r dev = c("png", "jpg", "tiff") ggplot(airquality, aes(x = Temp, y = Wind)) + geom_point() ```
Tip number 3: Specify some global options for all your code chunks
Writing that for each chunk can be pretty annoying, luckily there is a better way - you can specify all the options you want just once, using
knitr::set_opts(dev = "jpg").
Here is my setup chunk that I have for one of my papers at the moment:
```r setup-chunk, include=FALSE knitr::opts_chunk$set(dev = "png", dpi = 300, echo = FALSE, cache = TRUE) ```
This tells rmarkdown:
- Save all images as
- Save the PNGs at a nice high quality please, at
- Don’t print my code
echo = FALSE
- Save all my output the first time you run it, so I don’t ahve to wait forever for my rmarkdown document to run
I don’t want to get distracted with what
cache is, but very quickly, using
cache TRUE means that you don’t have to create
Tip Number 2: Name your code chunks
You can actually give each chunk of code a name:
```r my-amazing-plot, dev = c("png", "jpg", "tiff", "ps") ggplot(airquality, aes(x = Temp, y = Wind)) + geom_point() ```
It might seem trivial, but naming your code chunks has some nice side effects:
- It now saves that plot with the chunk name (instead of untitled-1)
If all chunks are named, this means that when you cache, you won’t get a slowdown, as it might need to re-reun untitled-1, when you change your code. - [evidence]()
Helps give you context when you are running your code 6 months later.
You can use the name of your code chunk in
dependson, if you have a specific reason
I’ve got some other little #rstats hacks (or rather, [crantrips]() that I like to use - I’ll write about them in a series in the future.
Finally, if you haven’t seen it yet, check out the new Rmarkdown book written by Yihui, JJ Allaire, and Garrett Grolemund “R Markdown: The Definitive Guide”