R Markdown: The Definitive Guide (2025)

Yihui Xie, J. J. Allaire, Garrett Grolemund

2023-12-30

Note: This book has been published by Chapman & Hall/CRC. The online version of this book is free to read here (thanks to Chapman & Hall/CRC), and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

R Markdown: The Definitive Guide (1)

The document format “R Markdown” was first introduced in the knitr package (Xie 2015, 2023b) in early 2012. The idea was to embed code chunks (of R or other languages) in Markdown documents. In fact, knitr supported several authoring languages from the beginning in addition to Markdown, including LaTeX, HTML, AsciiDoc, reStructuredText, and Textile. Looking back over the five years, it seems to be fair to say that Markdown has become the most popular document format, which is what we expected. The simplicity of Markdown clearly stands out among these document formats.

However, the original version of Markdown invented by John Gruber was often found overly simple and not suitable to write highly technical documents. For example, there was no syntax for tables, footnotes, math expressions, or citations. Fortunately, John MacFarlane created a wonderful package named Pandoc (http://pandoc.org) to convert Markdown documents (and many other types of documents) to a large variety of output formats. More importantly, the Markdown syntax was significantly enriched. Now we can write more types of elements with Markdown while still enjoying its simplicity.

In a nutshell, R Markdown stands on the shoulders of knitr and Pandoc. The former executes the computer code embedded in Markdown, and converts R Markdown to Markdown. The latter renders Markdown to the output format you want (such as PDF, HTML, Word, and so on).

R Markdown: The Definitive Guide (2)

The rmarkdown package (Allaire, Xie, Dervieux, McPherson, et al. 2023) was first created in early 2014. During the past four years, it has steadily evolved into a relatively complete ecosystem for authoring documents, so it is a good time for us to provide a definitive guide to this ecosystem now. At this point, there are a large number of tasks that you could do with R Markdown:

  • Compile a single R Markdown document to a report in different formats, such as PDF, HTML, or Word.

  • Create notebooks in which you can directly run code chunks interactively.

  • Make slides for presentations (HTML5, LaTeX Beamer, or PowerPoint).

  • Produce dashboards with flexible, interactive, and attractive layouts.

  • Build interactive applications based on Shiny.

  • Write journal articles.

  • Author books of multiple chapters.

  • Generate websites and blogs.

There is a fundamental assumption underneath R Markdown that users should be aware of: we assume it suffices that only a limited number of features are supported in Markdown. By “features,” we mean the types of elements you can create with native Markdown. The limitation is a great feature, not a bug. R Markdown may not be the right format for you if you find these elements not enough for your writing: paragraphs, (section) headers, block quotations, code blocks, (numbered and unnumbered) lists, horizontal rules, tables, inline formatting (emphasis, strikeout, superscripts, subscripts, verbatim, and small caps text), LaTeX math expressions, equations, links, images, footnotes, citations, theorems, proofs, and examples. We believe this list of elements suffice for most technical and non-technical documents. It may not be impossible to support other types of elements in R Markdown, but you may start to lose the simplicity of Markdown if you wish to go that far.

Epictetus once said, “Wealth consists not in having great possessions, but in having few wants.” The spirit is also reflected in Markdown. If you can control your preoccupation with pursuing typesetting features, you should be much more efficient in writing the content and can become a prolific author. It is entirely possible to succeed with simplicity. Jung Jae-sung was a legendary badminton player with a remarkably simple playing style: he did not look like a talented player and was very short compared to other players, so most of the time you would just see him jump three feet off the ground and smash like thunder over and over again in the back court until he beats his opponents.

Please do not underestimate the customizability of R Markdown because of the simplicity of its syntax. In particular, Pandoc templates can be surprisingly powerful, as long as you understand the underlying technologies such as LaTeX and CSS, and are willing to invest time in the appearance of your output documents (reports, books, presentations, and/or websites). As one example, you may check out the PDF report of the 2017 Employer Health Benefits Survey. It looks fairly sophisticated, but was actually produced via bookdown (Xie 2016), which is an R Markdown extension. A custom LaTeX template and a lot of LaTeX tricks were used to generate this report. Not surprisingly, this very book that you are reading right now was also written in R Markdown, and its full source is publicly available in the GitHub repository https://github.com/rstudio/rmarkdown-book.

R Markdown documents are often portable in the sense that they can be compiled to multiple types of output formats. Again, this is mainly due to the simplified syntax of the authoring language, Markdown. The simpler the elements in your document are, the more likely that the document can be converted to different formats. Similarly, if you heavily tailor R Markdown to a specific output format (e.g., LaTeX), you are likely to lose the portability, because not all features in one format work in another format.

Last but not least, your computing results will be more likely to be reproducible if you use R Markdown (or other knitr-based source documents), compared to the manual cut-and-paste approach. This is because the results are dynamically generated from computer source code. If anything goes wrong or needs to be updated, you can simply fix or update the source code, compile the document again, and the results will be automatically updated. You can enjoy reproducibility and convenience at the same time.

References

Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2023. Rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.

Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.name/knitr/.

———. 2016. Bookdown: Authoring Books and Technical Documents with R Markdown. Boca Raton, Florida: Chapman; Hall/CRC. https://github.com/rstudio/bookdown.

———. 2023b. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.

R Markdown: The Definitive Guide (2025)

FAQs

What are the cons of R Markdown? ›

There are a few disadvantages to R Markdown. No track changes - Even if you're lucky to have an advisor who will review a . Rmd file, you won't get nice track changes like in Word. There are alternative to this (version control helps) but not are quite as easy as track changes.

What is $$ in Markdown? ›

Math inside RMarkdown

In side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”.

What is the difference between R Markdown and Markdown? ›

A plain Markdown post is directly rendered to HTML through Goldmark (a Markdown renderer written in the Go language and adopted by Hugo). An R Markdown post is first compiled to Markdown through the R packages rmarkdown and bookdown, which means you can use most features of bookdown's Markdown extensions in blogdown.

How to do calculations in R Markdown? ›

R Markdown allows you to insert a calculated value into the middle of your text. This is accomplished by putting a single backtick followed by the letter “r” (lower case, with no space after the backtick) and then your code, and ended with another backtick. Or we could put that calculation inside a sentence.

What is better than Markdown? ›

The AsciiDoc syntax is more concise than (or at least as concise as) Markdown. At the same time, AsciiDoc offers power and flexibility without requiring the use of HTML or “flavors” for essential syntax such as tables, description lists, admonitions (tips, notes, warnings, etc.) and table of contents.

Is Markdown still relevant? ›

So, if you'd prefer, you can still use HTML tags when working with Markdown. Markdown is used almost everywhere, from GitHub to Slack. It's the unofficial text writing and formatting standard on big coding sites, like coding repositories. Most engineering readme files are written with and formatted using Markdown.

What is ### in Markdown? ›

To create a heading, add number signs ( # ) in front of a word or phrase. The number of number signs you use should correspond to the heading level. For example, to create a heading level three ( <h3> ), use three number signs (e.g., ### My Header ).

Who invented Markdown? ›

John Gruber created Markdown in 2004 as an easy-to-read markup language. Markdown is widely used for blogging and instant messaging, and also used elsewhere in online forums, collaborative software, documentation pages, and readme files.

Can I use HTML in Markdown? ›

Span-level HTML tags — e.g. <span> , <cite> , or <del> — can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you'd prefer to use HTML <a> or <img> tags instead of Markdown's link or image syntax, go right ahead.

What is the alternative to RMarkdown? ›

Alternatives to R Markdown
  • Inkdrop. Inkdrop. $4.16 per month. ...
  • blogdown. blogdown. This book introduces blogdown, an R package that teaches you how to create websites with Hugo and R Markdown. ...
  • Markdown Monster. Markdown Monster. ...
  • StackEdit. StackEdit. ...
  • Quiver. Quiver. ...
  • MWeb. MWeb. ...
  • ghostwriter. ghostwriter. ...
  • Remarkable. Remarkable.

Should I use R Markdown or notebook? ›

R Notebooks are better suited for smaller analyses or exploratory work. R Markdown is better suited for larger-scale projects with more complex requirements.

Is RMarkdown better than LaTeX? ›

R Markdown is certainly not the best possible document format for authoring or typesetting documents. Simplicity is both its advantage and disadvantage. LaTeX is much more powerful than Markdown in terms of typesetting at the price of more commands to be typed.

How do I write codes in R Markdown? ›

Code results can be inserted directly into the text of a . Rmd file by enclosing the code with `r ` . The file below uses `r ` twice to call colorFunc , which returns “heat. colors.” You can open the file here in RStudio Cloud.

Can you create a table in R Markdown? ›

Using the rmarkdown::paged_table() function to create a page-able version of a data frame. Use the gtsummary and gt packages to create to create publication-ready analytical and summary tables.

How to do math in R Markdown? ›

To write mathematical formulas in R Markdown, we use LaTeX syntax. This is enclosed in dollar signs ( $ ) for inline formulas and double dollar signs ( $$ ) for display formulas1.

What are the disadvantages of R? ›

Does R Have Any Drawbacks?
  • It's a complicated language. R has a steep learning curve. ...
  • It's not as secure. R doesn't have basic security measures. ...
  • It's slow. R is slower than other programming languages like Python or MATLAB.
  • It takes up a lot of memory. ...
  • It doesn't have consistent documentation/package quality.
Aug 13, 2024

What is Markdown not used for? ›

Lack of Semantic Meaning.

With Markdown you can only write text. It means that if you need to grab the reader's attention with some kind of notes or tips, you have to embed HTML. The lack of semantic support is a problem for a few reasons: Markdown is now dependent on specific HTML classes, and page design.

Is RMarkdown useful? ›

This is exactly what R markdown allows you to do. Some of the advantages of using R markdown include: Explicitly links your data with your R code and output creating a fully reproducible workflow. ALL of the R code used to explore, summarise and analyse your data can be included in a single easy to read document.

What are the disadvantages of investing in R&D? ›

Disadvantages. One of the major drawbacks to R&D is the cost. First, there is the financial expense as it requires a significant investment of cash upfront. This can include setting up a separate R&D department, hiring talent, and product and service testing, among others.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Roderick King

Last Updated:

Views: 5447

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.