R Markdown Cookbook  book cover
1st Edition

R Markdown Cookbook

ISBN 9780367563820
Published October 20, 2020 by Chapman and Hall/CRC
360 Pages

FREE Standard Shipping
USD $94.95

Prices & shipping based on shipping country


Book Description

This new book written by the developers of R Markdown is an essential reference that will help users learn and make full use of the software.

Those new to R Markdown will appreciate the short, practical examples that address the most common issues users encounter. Frequent users will also benefit from the wide ranging tips and tricks that expose ‘hidden’ features, support customization and demonstrate the many new and varied applications of the software.

After reading this book users will learn how to:

  • Enhance your R Markdown content with diagrams, citations, and dynamically generated text
  • Streamline your workflow with child documents, code chunk references, and caching
  • Control the formatting and layout with Pandoc markdown syntax or by writing custom HTML and LaTeX templates
  • Utilize chunk options and hooks to fine-tune how your code is processed
  • Switch between different language engineers to seamlessly incorporate python, D3, and more into your analysis

Table of Contents

  1. Installation
  2. Use a Pandoc version not bundled with the RStudio IDE

    Install LaTeX (TinyTeX) for PDF reports

    Install missing LaTeX packages

  3. Conceptual Overview
  4. What happens when we render?

    R Markdown anatomy

    YAML metadata


    Code chunks

    Document body

    What can we change to change the results?

  5. Basics
  6. Code chunks and inline R code

    Write Markdown in the RStudio visual editor

    Render an R script to a report

    Convert R Markdown to R script

    R Markdown Notebooks

  7. Document Elements
  8. Insert page breaks

    Set the document title dynamically

    Access the document metadata in R code

    Unnumbered sections

    Bibliographies and citations

    Changing citation style

    Add an item to bibliography without using it

    Add all items to bibliography

    Include appendix after bibliography (*)

    Generate R package citations

    Cross-referencing within documents

    Update the date automatically

    Multiple authors in a document

    Numbered figure captions

    Combine words into a comma-separated phrase

    Preserve a large number of linebreaks

    Convert models to equations

    Create an animation from multiple R plots

    Create diagrams

    Basic diagrams

    Adding parameters to plots

    Other packages for making diagrams

    Escape special characters

    Comment out text

    Omit a heading in the table of contents

    Put together all code in the appendix (*)

    Manipulate Markdown via Pandoc Lua filters (*)

  9. Formatting
  10. Font color

    Using an R function to write raw HTML or LaTeX code

    Using a Pandoc Lua filter (*)

    Indent text

    Control the width of text output

    Control the size of plots/images

    Figure alignment

    Verbatim code chunks

    Show a verbatim inline expression

    Line numbers for code blocks (*)

    Multi-column layout (*)

  11. LaTeX Output
  12. Add LaTeX code to the preamble

    Pandoc options for LaTeX output

    Add logo to title page

    Include additional LaTeX packages

    Loading LaTeX packages

    Example packages

    Control the placement of figures

    Floating environments

    Prevent figures from floating

    Force floats forwards

    Adjust LaTeX placement rules (*)

    LaTeX sub-figures

    Render documents containing Unicode characters

    Generate a LaTeX fragment

    Add custom headers and footers (*)

    Use a custom Pandoc LaTeX template (*)

    Write raw LaTeX code

    For hardcore LaTeX users (*)

  13. HTML Output
  14. Apply custom CSS

    Center section headings

    Style code blocks and text output

    Scrollable code blocks (*)

    Fold all code blocks but show some initially

    Put content in tabs

    Embed the Rmd source file in the HTML output file

    Embed arbitrary files in the HTML output file

    Use a custom HTML template (*)

    Include the content of an existing HTML file (*)

    Add a custom browser icon

    Use the <details> disclosure element

    Sharing HTML output on the web

    R-specific services

    Static website services

    Improve accessibility of HTML pages

    For hardcore HTML users (*)

  15. Word
  16. Custom Word templates

    The two-way workflow between R Markdown and Word

    Style individual elements

  17. Multiple Output Formats
  18. LaTeX or HTML output

    Display HTML widgets

    Embed a web page

    Multiple figures side-by-side

    Write raw content (*)

    Custom blocks (*)


    Adding a shaded box

    Including icons

  19. Tables
  20. The function knitr::kable()

    Supported table formats

    Change column names

    Specify column alignment

    Add a table caption

    Format numeric columns

    Display missing values

    Escape special characters

    Multiple tables side by side

    Generate multiple tables from a for-loop (*)

    Customize LaTeX tables (*)

    Customize HTML tables (*)

    The kableExtra package

    Set the font size

    Style specific rows/columns

    Group rows/columns

    Scaling down wide tables in LaTeX

    Other packages for creating tables

  21. Chunk Options
  22. Use variables in chunk options

    Do not stop on error

    Multiple graphical output formats for the same plot

    Cache time-consuming code chunks

    Cache a code chunk for multiple output formats

    Cache large objects

    Hide code, text output, messages, or plots

    Hide everything from a chunk

    Collapse text output blocks into source blocks

    Reformat R source code

    Output text as raw Markdown content (*)

    Remove leading hashes in text output

    Add attributes to text output blocks (*)

    Post-process plots (*)

    High-quality graphics (*)

    Step-by-step plots with low-level plotting functions (*)

    Customize the printing of objects in chunks (*)

    Option hooks (*)

  23. Output Hooks (*)
  24. Redact source code

    Add line numbers to source code

    Scrollable text output

    Truncate text output

    Output figures in the HTML format

  25. Chunk Hooks (*)
  26. Crop plots

    Optimize PNG plots

    Report how much time each chunk takes to run

    Show the chunk header in the output

    Embed an interactive D plot with rgl

  27. Miscellaneous knitr Tricks
  28. Reuse code chunks

    Embed one chunk in another chunk (*)

    Use the same chunk label in another chunk

    Use reference labels (*)

    Use an object before it is created (*)

    Exit knitting early

    Generate a plot and display it elsewhere

    Modify a plot in a previous code chunk

    Save a group of chunk options and reuse them (*)

    Use knitr::knit_expand() to generate Rmd source

    Allow duplicate labels in code chunks (*)

    A more transparent caching mechanism

    Invalidate the cache by changing code in the expression

    Invalidate the cache by changes in global variables

    Keep multiple copies of the cache

    Comparison with knitr’s caching

  29. Other Languages
  30. Register a custom language engine (*)

    Run Python code and interact with Python

    Execute content conditionally via the asis engine

    Execute Shell scripts

    Visualization with D

    Write the chunk content to a file via the cat engine

    Write to a CSS file

    Include LaTeX code in the preamble

    Write YAML data to a file and also display it

    Run SAS code

    Run Stata code

    Create graphics with Asymptote

    Generate data in R and read it in Asymptote

    Style HTML pages with Sass/SCSS

  31. Managing Projects
  32. Source external R scripts

    Read external scripts into a chunk

    Read multiple code chunks from an external script (*)

    Child documents (*)

    Keep the plot files

    The working directory for R code chunks

    R package vignettes

    R Markdown templates in R packages

    Template use-cases

    Template set-up

    Write books and long-form reports with bookdown

    Build websites with blogdown

  33. Workflow

          Use RStudio keyboard shortcuts

          Spell check R Markdown

          Render R Markdown with rmarkdown::render()

          Parameterized reports

          Customize the Knit button (*)

          Collaborate on Rmd documents through Google Drive

          Organize an R Markdown project into a research website with workflowr

          Send Emails based on R Markdown


View More



Yihui Xie is a software engineer at RStudio. He has authored and co-authored several R packages, including knitr, rmarkdown, bookdown, blogdown, pagedown, tinytex, and xaringan. He has published R Markdown: The Definitive Guide, Dynamic Documents with R and knitr, bookdown: Authoring Books and Technical Documents with R Markdown, and blogdown: Creating Websites with R Markdown."

Christophe Dervieux is an active member of the R community. Working with R since almost 10 years, he is interested in helping others get the most from R. As an R developer, he is a regular contributor to several R packages, such as "bookdown", "rmarkdown", and "knitr".

Emily Riederer is an active R user and developer. She has previously contributed to "knitr" and is best known for her article on "R Markdown Driven Development" based on her experience promoting R adoption in industry.


"This book will be a popular addition on the desk of many professionals who regularly produce technical documents in the R language."
~Carl Boettiger, University of California, Berkeley

"Novices will benefit from reading this book because they will quickly find high quality solutions to the most common issues that users encounter. Experts will benefit because it will save them from constantly trying to rediscover ‘that thread that explains how to do that one thing.’"
~ Dr. John Blischak, University of Chicago

"Though I am a frequent and longtime user of RMarkdown, there were multiple points where I learned new things and capabilities that I’d never seen. Sometimes incredible features are ‘hidden"; this book eposes features and tricks that this seasoned RMarkdown user was surprised and excited by."
~ Sharla Gelfand, R and Shiny Developer

"This book is useful for R stats users who, while using RMarkdown, come across certain problems that can be solved. It also provides advanced users with additional options to further customize RMarkdown to their personal needs. The many subchapters address a number of issues that a large majority of users can use as a reference book for their needs. There are examples for every user, no matter what previous knowledge is available and therefore this book is useful for any user group of markdown."
~Dr. Johannes Friedrich

"This book is interesting for a wide level of RMarkdown users."
~MSc. Yasumoto, Atsushi, HACARUS Inc.