R MarkdownOverviewR Markdown is a format that enables easy authoring of reproducible web reports from R. It combines the core syntax of Markdown (an easy-to-write plain text format for web content) with embedded R code chunks that are run so their output can be included in the final document. The concept of R Markdown is similar to Sweave (a system built-in to R for combining R with LaTeX). In Sweave, Rnw files are "weaved" into TeX files that are then compiled into PDFs. In R Markdown, Rmd files are similarly "weaved" into plain markdown (.md) files that are then compiled into HTML documents. The R Markdown syntax is described in detail below. At a high-level it is a combination of the following:
ImplementationThe implementation of R Markdown is provided by two packages:
For example, to run the R code chunks inside an Rmd file and then convert the resulting markdown file into HTML you would execute the following:
Note that this can also be accomplished in one step by calling RStudio also implements support for rendering R Markdown files into HTML using the Knit HTML commands. This produces a result equivalent to the above code. SyntaxThe following is a detailed description of the R Markdown syntax. Note that the first two sections covering Core Markdown and R Code Chunks are always applicable. The remaining sections apply to the rendering of markdown to HTML as implemented by RStudio and the markdown package. If you are using an alternate markdown program such as Pandoc please consult its documentation for information on comparable extensions. Core MarkdownThe core markdown syntax defines plain-text formatting commands for a wide variety of constructs including headers, phrase emphasis, ordered and unordered lists, links, images, and inline code. R Markdown implements all of the core markdown syntax (with two exceptions noted below in the Github/Sundown Extensions section). Please reference the core markdown syntax definition for a detailed description of supported elements. Embedded R CodeWithin an R Markdown file, R code chunks can be embedded using fenced code regions:
R code chunks can also have a label and specify processing options:
You can also evaluate R expressions inline by enclosing the expression within a single back-tick qualified with 'r'. For example:
When R code is displayed in the resulting HTML file, it is given a distinct background color and syntax highlighted. Textual output resulting from R code is shown within a bordered frame. Sundown ExtensionsThe markdown package uses the Sundown library for rendering markdown to HTML (Sundown is a project maintained by Github). Sundown supports core markdown, a set of extensions known as Github flavored markdown, as well several other extensions. The Github flavored markdown extensions include:
Additional extensions include:
Finally, selected ASCII character sequences are converted into typographic HTML entities:
LaTeX and MathML EquationsYou can embed LaTeX or MathML equations in R Markdown files using the following syntax:
The article on Equations in R Markdown includes more detailed information on embedded equations. Image BundlingWhen converting from markdown to HTML, images that are referenced using relative hrefs are bundled directly into the generated HTML file. This creates a standalone web page that can be shared without needing to package up any additional files. You can attach the HTML file to an email, share it using a public folder, or deploy it to a web server as a single HTML file. Image bundling is performed by base64 encoding the image and then inlining the encoded image using a data URI. Text EncodingThe Sundown library reads and writes content using UTF-8 encoding. It is therefore most convenient to use UTF-8 as the encoding for Rmd files. Note that RStudio supports the use of other encodings for Rmd files (it converts them to UTF-8 prior to sending them to Sundown) however if you are using the markdown package directly you'll need to perform this conversion manually. Related Topics |
|