RMarkdown Tips and Tricks

This document provides a collection of my tweets containing tips and tricks related to RMarkdown. This document is mostly to organize my own thoughts since it became increasingly difficult to find the tweet I had in mind and also to remember which tip I had shared and which I wanted to share. And, although itโ€™s for personal use, I am hoping that others might also find these tips and tricks useful. I will keep updating this document as I add new tweets.

If you are reading this on GitHub README, see this webpage instead to see the tweets rendered properly: https://indrajeetpatil.github.io/RmarkdownTips/

All the source code for this document can be found on GitHub: https://github.com/IndrajeetPatil/RmarkdownTips


Most of these tips are from Yihui Xieโ€™s books and I would highly recommend that you check them out. Except for the knitr, all other books are available online for free.

I would also like to thank Christophe Dervieux, who has patiently answered my Rmarkdown-related questions, queries, and comments on Twitter, GitHub, and StackOverflow. His kindness and patience is boundless, not to mention his knowledge of the markdown-universe.

Tweets with tips and tricks

These are in no particular order, and, like my brain, these are scattered all over the place. ๐Ÿ˜…

Embed one chunk in another chunk

Reusing code chunks in \#rmarkdown (using `<<>>`) is a really cool feature, esp.ย in teaching contexts! โ™ป๏ธ

You can introduce students to code components piece-by-piece without having to repeat yourself in the underlying document! ๐Ÿ‘ฉโ€๐Ÿซ

For more, see-https://t.co/Hup2mjd6dl\#rstats pic.twitter.com/KCdEhs2ACv

โ€” Indrajeet Patil (@patilindrajeets) July 9, 2021

Render R Markdown with rmarkdown::render()

Sometimes you may wish to render an \#rmarkdown document from a code chunk of *another* document.
E.g., if you're writing a paper and want to render the supplementary info document from the main document.

Doing so is as simple as this ๐Ÿ‘‡! ๐Ÿ™Œ

Ref. https://t.co/FDfAZ2q7WA\#rstats pic.twitter.com/9In6J8dlNl

โ€” Indrajeet Patil (@patilindrajeets) July 13, 2021

Child documents

Sometimes you want to:
โ™ป๏ธ reuse text (e.g.ย methods, session info) across documents
โœ‚๏ธ break a long document into smaller ones

In \#rmarkdown, you can create a โ€œchildโ€ document and insert its contents into a โ€œparentโ€ document! ๐Ÿ‘ฉโ€๐Ÿ‘ฆhttps://t.co/QOtOxwwmbN

๐ŸŽฅ 1-min demo๐Ÿ‘‡\#rstats pic.twitter.com/tMzF6HUuTb

โ€” Indrajeet Patil (@patilindrajeets) July 8, 2021

Line numbers for code blocks

If you want to, \#rmarkdown provides a chunk option to easily add line numbers to source code and output text! ๐Ÿงฎ

Ref: https://t.co/bM2oL6kpqt

- You'll need to use syntax highlighting theme `tango`.
- Although only HTML output is shown, it works for PDF as well.\#rstats pic.twitter.com/8wKwA9uTwt

โ€” Indrajeet Patil (@patilindrajeets) July 6, 2021

Knitting with Infinite Moon Reader

Maybe I was the only one who didn't know this for a long time, but you don't have to knit \#rmarkdown document for a preview *every time* you make a change.

Just run `xaringan::infinite_moon_reader()` function, and the preview will change after saving the document! ๐Ÿ™Œ๐Ÿ•บ\#rstats pic.twitter.com/Tz3Y6hNGh3

โ€” Indrajeet Patil (@patilindrajeets) February 27, 2021

Side-by-side plots in code chunk

If you've got a code chunk in \#rmarkdown that produces multiple plots, they will by default be shown sequentially.

But sometimes one may want them to be instead displayed side-by-side ๐Ÿ‘ฏ

Below is a 1-min demo of how to do so ๐Ÿ™Œ
For more, see: https://t.co/KXLAuEXP78\#rstats pic.twitter.com/cNBn4WnJWZ

โ€” Indrajeet Patil (@patilindrajeets) May 11, 2021