Using Markdown formatting, websites, documents, images and videos can be inserted into almost any location. Common examples include content blocks and hint text on fields. In the words of its creator (John Gruber), 'The idea is that a Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked.
Interactive documents are a new way to build Shiny apps. An interactive document is an R Markdown file that contains Shiny widgets and outputs. You write the report in markdown, and then launch it as an app with the click of a button.
This article will show you how to write an R Markdown report.
The companion article, Introduction to interactive documents, will show you how to turn an R Markdown report into an interactive document with Shiny components.
R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below.
R Markdown files are designed to be used with the rmarkdown
package. rmarkdown
comes installed with the RStudio IDE, but you can acquire your own copy of rmarkdown
from CRAN with the command
R Markdown files are the source code for rich, reproducible documents. You can transform an R Markdown file in two ways.
knit - You can knit the file. The rmarkdown
package will call the knitr
package. knitr
will run each chunk of R code in the document and append the results of the code to the document next to the code chunk. This workflow saves time and facilitates reproducible reports.
Consider how authors typically include graphs (or tables, or numbers) in a report. The author makes the graph, saves it as a file, and then copy and pastes it into the final report. This process relies on manual labor. If the data changes, the author must repeat the entire process to update the graph.
In the R Markdown paradigm, each report contains the code it needs to make its own graphs, tables, numbers, etc. The author can automatically update the report by re-knitting.
convert - You can convert the file. The rmarkdown
package will use the pandoc
program to transform the file into a new format. For example, you can convert your .Rmd file into an HTML, PDF, or Microsoft Word file. You can even turn the file into an HTML5 or PDF slideshow. rmarkdown
will preserve the text, code results, and formatting contained in your original .Rmd file.
Conversion lets you do your original work in markdown, which is very easy to use. You can include R code to knit, and you can share your document in a variety of formats.
In practice, authors almost always knit and convert their documents at the same time. In this article, I will use the term render to refer to the two step process of knitting and converting an R Markdown file.
You can manually render an R Markdown file with rmarkdown::render()
. This is what the above document looks like when rendered as a HTML file.
In practice, you do not need to call rmarkdown::render()
. You can use a button in the RStudio IDE to render your reprt. R Markdown is heavily integrated into the RStudio IDE.
To create an R Markdown report, open a plain text file and save it with the extension .Rmd. You can open a plain text file in your scripts editor by clicking File > New File > Text File in the RStudio toolbar.
Be sure to save the file with the extension .Rmd. The RStudio IDE enables several helpful buttons when you save the file with the .Rmd extension. You can save your file by clicking File > Save in the RStudio toolbar.
R Markdown reports rely on three frameworks
knitr
for embedded R codeThe sections below describe each framework.
.Rmd files are meant to contain text written in markdown. Markdown is a set of conventions for formatting plain text. You can use markdown to indicate
The conventions of markdown are very unobtrusive, which make Markdown files easy to read. The file below uses several of the most useful markdown conventions.
The file demonstrates how to use markdown to indicate:
headers - Place one or more hashtags at the start of a line that will be a header (or sub-header). For example, # Say Hello to markdown
. A single hashtag creates a first level header. Two hashtags, ##
, creates a second level header, and so on.
italicized and bold text - Surround italicized text with asterisks, like this *without realizing it*
. Surround bold text with two asterisks, like this **easy to use**
.
lists - Group lines into bullet points that begin with asterisks. Leave a blank line before the first bullet, like this
hyperlinks - Surround links with brackets, and then provide the link target in parentheses, like this [Github](www.github.com)
.
You can learn about more of markdown’s conventions in the Markdown Quick Reference guide, which comes with the RStudio IDE.
To access the guide, open a .md or .Rmd file in RStudio. Then click the question mark that appears at the top of the scripts pane. Next, select “Markdown Quick Reference”. RStudio will open the Markdown Quick Reference guide in the Help pane.
To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want.
When you click the button, rmarkdown
will duplicate your text in the new file format. rmarkdown
will use the formatting instructions that you provided with markdown syntax.
Once the file is rendered, RStudio will show you a preview of the new output and save the output file in your working directory.
Here is how the markdown script above would look in each output format.
Note: RStudio does not build PDF and Word documents from scratch. You will need to have a distribution of Latex installed on your computer to make PDFs and Microsoft Word (or a similar program) installed to make Word files.
The knitr
package extends the basic markdown syntax to include chunks of executable R code.
When you render the report, knitr
will run the code and add the results to the output file. You can have the output display just the code, just the results, or both.
To embed a chunk of R code into your report, surround the code with two lines that each contain three backticks. After the first set of backticks, include {r}
, which alerts knitr
that you have included a chunk of R code. The result will look like this
When you render your document, knitr
will run the code and append the results to the code chunk. knitr
will provide formatting and syntax highlighting to both the code and its results (where appropriate).
As a result, the markdown snippet above will look like this when rendered (to HTML).
To omit the results from your final report (and not run the code) add the argument eval = FALSE
inside the brackets and after r
. This will place a copy of your code into the report.
To omit the code from the final report (while including the results) add the argument echo = FALSE
. This will place a copy of the results into your report.
echo = FALSE
is very handy for adding plots to a report, since you usually do not want to see the code that generates the plot.
echo
and eval
are not the only arguments that you can use to customize code chunks. You can learn more about formatting the output of code chunks at the rmarkdown and knitr websites.
To embed R code in a line of text, surround the code with a pair of backticks and the letter r
, like this.
knitr
will replace the inline code with its result in your final document (inline code is always replaced by its result). The result will appear as if it were part of the original text. For example, the snippet above will appear like this:
You can use a YAML header to control how rmarkdown
renders your .Rmd file. A YAML header is a section of key: value
pairs surrounded by ---
marks, like below
The output:
value determines what type of output to convert the file into when you call rmarkdown::render()
. Note: you do not need to specify output:
if you render your file with the RStudio IDE knit button.
output:
recognizes the following values:
html_document
, which will create HTML output (default)pdf_document
, which will create PDF outputword_document
, which will create Word outputIf you use the RStudio IDE knit button to render your file, the selection you make in the gui will override the output:
setting.
You can also use the output:
value to render your document as a slideshow.
output: ioslides_presentation
will create an ioslides (HTML5) slideshowoutput: beamer_presentation
will create a beamer (PDF) slideshowNote: The knit button in the RStudio IDE will update to show slideshow options when you include one of the above output values and save your .Rmd file.
rmarkdown
will convert your document into a slideshow by starting a new slide at each header or horizontal rule (e.g., ***
).
Visit rmakdown.rstudio.com to learn about more YAML options that control the render process.
R Markdown documents provide quick, reproducible reporting from R. You write your document in markdown and embed executable R code chunks with the knitr
syntax.
You can update your document at any time by re-knitting the code chunks.
You can then convert your document into several common formats.
R Markdown documents implement Donald’s Knuth’s idea of literate programming and take the manual labor out of writing and maintaining reports. Moreover, they are quick to learn. You already know ecnough about markdown, knitr, and YAML to begin writing your own R Markdown reports.
In the next article, Introduction to interactive documents, you will learn how to add interactive Shiny components to an R Markdown report. This creates a quick workflow for writing light-weight Shiny apps.
To learn more about R Markdown and interactive documents, please visit rmarkdown.rstudio.com.
-->This article provides an alphabetical reference for writing Markdown for docs.microsoft.com (Docs).
Markdown is a lightweight markup language with plain text formatting syntax. Docs supports CommonMark compliant Markdown parsed through the Markdig parsing engine. Docs also supports custom Markdown extensions that provide richer content on the Docs site.
You can use any text editor to write Markdown, but we recommend Visual Studio Code with the Docs Authoring Pack. The Docs Authoring Pack provides editing tools and preview functionality that lets you see what your articles will look like when rendered on Docs.
Alerts are a Markdown extension to create block quotes that render on docs.microsoft.com with colors and icons that indicate the significance of the content. The following alert types are supported:
These alerts look like this on docs.microsoft.com:
Note
Information the user should notice even if skimming.
Tip
Optional information to help a user be more successful.
Important
Essential information required for user success.
Caution
Negative potential consequences of an action.
Warning
Dangerous certain consequences of an action.
If you use angle brackets in text in your file--for example, to denote a placeholder--you need to manually encode the angle brackets. Otherwise, Markdown thinks that they're intended to be an HTML tag.
For example, encode <script name>
as <script name>
or <script name>
.
Angle brackets don't have to be escaped in text formatted as inline code or in code blocks.
If you copy from Word into a Markdown editor, the text might contain 'smart' (curly) apostrophes or quotation marks. These need to be encoded or changed to basic apostrophes or quotation marks. Otherwise, you end up with things like this when the file is published: It’s
Here are the encodings for the 'smart' versions of these punctuation marks:
“
”
’
‘
Blockquotes are created using the >
character:
The preceding example renders as follows:
This is a blockquote. It is usually rendered indented and with a different background color.
To format text as bold, enclose it in two asterisks:
To format text as italic, enclose it in a single asterisk:
To format text as both bold and italic, enclose it in three asterisks:
Docs Markdown supports the placement of code snippets both inline in a sentence and as a separate 'fenced' block between sentences. For more information, see How to add code to docs.
The columns Markdown extension gives Docs authors the ability to add column-based content layouts that are more flexible and powerful than basic Markdown tables, which are only suited for true tabular data. You can add up to four columns, and use the optional span
attribute to merge two or more columns.
The syntax for columns is as follows:
Columns should only contain basic Markdown, including images. Headings, tables, tabs, and other complex structures shouldn't be included. A row can't have any content outside of column.
For example, the following Markdown creates one column that spans two column widths, and one standard (no span
) column:
This renders as follows:
This is a 2-span column with lots of text.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum mollis nuncornare commodo. Nullam ac metus imperdiet, rutrum justo vel, vulputate leo. Donecrutrum non eros eget consectetur.
Docs supports six levels of Markdown headings:
#
and heading text.<h1>
, aren't recommended, and in some cases will cause build warnings.Although Markdown supports inline HTML, HTML isn't recommended for publishing to Docs, and except for a limited list of values will cause build errors or warnings.
The following file types are supported by default for images:
The basic Markdown syntax to embed an image is:
Where <alt text>
is a brief description of the image and <folder path>
is a relative path to the image. Alternate text is required for screen readers for the visually impaired. It's also useful if there's a site bug where the image can't render.
Underscores in alt text aren't rendered properly unless you escape them by prefixing them with a backslash (_
). However, don't copy file names for use as alt text. For example, instead of this:
Write this:
The Docs custom :::image:::
extension supports standard images, complex images, and icons.
For standard images, the older Markdown syntax will still work, but the new extension is recommended because it supports more powerful functionality, such as specifying a localization scope that's different from the parent topic. Other advanced functionality, such as selecting from the shared image gallery instead of specifying a local image, will be available in the future. The new syntax is as follows:
If type='content'
(the default), both source
and alt-text
are required.
You can also use this extension to add an image with a long description that is read by screen readers but not rendered visually on the published page. Long descriptions are an accessibility requirement for complex images, such as graphs. The syntax is the following:
If type='complex'
, source
, alt-text
, a long description, and the :::image-end:::
tag are all required.
Sometimes the localization scope for an image is different from that of the article or module that contains it. This can cause a bad global experience: for example, if a screenshot of a product is accidentally localized into a language the product isn't available in. To prevent this, you can specify the optional loc-scope
attribute in images of types content
and complex
.
The image extension supports icons, which are decorative images and should not have alt text. The syntax for icons is:
If type='icon'
, only source
should be specified.
Where markdown files need to be repeated in multiple articles, you can use an include file. The includes feature instructs Docs to replace the reference with the contents of the include file at build time. You can use includes in the following ways:
An inline or block include file is a Markdown (.md) file. It can contain any valid Markdown. Include files are typically located in a common includes subdirectory, in the root of the repository. When the article is published, the included file is seamlessly integrated into it.
Block include is on its own line:
Inline include is within a line:
Where <title>
is the name of the file and <filepath>
is the relative path to the file. INCLUDE
must be capitalized and there must be a space before the <title>
.
Here are requirements and considerations for include files:
<repo>
/includes/media folder. The media directory should not contain any images in its root. If the include does not have images, a corresponding media directory is not required.For information on syntax for links, see Use links in documentation.
To create a numbered list, you can use all 1s. The numbers are rendered in ascending order as a sequential list when published. For increased source readability, you can increment your lists manually.
Don't use letters in lists, including nested lists. They don't render correctly when published to Docs. Nested lists using numbers will render as lowercase letters when published. For example:
This renders as follows:
To create a bulleted list, use -
or *
followed by a space at the beginning of each line:
This renders as follows:
Whichever syntax you use, -
or *
, use it consistently within an article.
Checklists are available for use on Docs via a custom Markdown extension:
This example renders on Docs like this:
Use checklists at the beginning or end of an article to summarize 'What will you learn' or 'What have you learned' content. Do not add random checklists throughout your articles.
You can use a custom extension to add a next step action button to Docs pages.
The syntax is as follows:
For example:
This renders as follows:
You can use any supported link in a next step action, including a Markdown link to another web page. In most cases, the next action link will be a relative link to another file in the same docset.
You can use the custom no-loc
Markdown extension to identify strings of content that you would like the localization process to ignore.
All strings called out will be case-sensitive; that is, the string must match exactly to be ignored for localization.
To mark an individual string as non-localizable, use this syntax:
For example, in the following, only the single instance of Document
will be ignored during the localization process:
Note
Use to escape special characters:
You can also use metadata in the YAML header to mark all instances of a string within the current Markdown file as non-localizable:
Note
The no-loc metadata is not supported as global metadata in docfx.json file. The localization pipeline doesn't read the docfx.json file, so the no-loc metadata must be added into each individual source file.
In the following example, both in the metadata title
and the Markdown header the word Document
will be ignored during the localization process.
In the metadata description
and the Markdown main content the word document
is localized, because it does not start with a capital D
.
Selectors are UI elements that let the user switch between multiple flavors of the same article. They are used in some doc sets to address differences in implementation across technologies or platforms. Selectors are typically most applicable to our mobile platform content for developers.
Because the same selector Markdown goes in each article file that uses the selector, we recommend placing the selector for your article in an include file. Then you can reference that include file in all your article files that use the same selector.
There are two types of selectors: a single selector and a multi-selector.
... will be rendered like this:
... will be rendered like this:
You should only use subscript or superscript when necessary for technical accuracy, such as when writing about mathematical formulas. Don't use them for non-standard styles, such as footnotes.
For both subscript and superscript, use HTML:
This renders as follows:
Hello This is subscript!
This renders as follows:
Goodbye This is superscript!
The simplest way to create a table in Markdown is to use pipes and lines. To create a standard table with a header, follow the first line with dashed line:
This renders as follows:
This is | a simple | table header |
---|---|---|
table | data | here |
it doesn't | actually | have to line up nicely! |
You can align the columns by using colons:
Renders as follows:
Fun | With | Tables |
---|---|---|
left-aligned column | right-aligned column | centered column |
$100 | $100 | $100 |
$10 | $10 | $10 |
$1 | $1 | $1 |
Tip
The Docs Authoring Extension for VS Code makes it easy to add basic Markdown tables!
You can also use an online table generator.
Long words in a Markdown table might make the table expand to the right navigation and become unreadable. You can solve that by allowing Docs rendering to automatically insert line breaks within words when needed. Just wrap up the table with the custom class [!div]
.
Here is a Markdown sample of a table with three rows that will be wrapped by a div
with the class name mx-tdBreakAll
.
It will be rendered like this:
Name | Syntax | Mandatory for silent installation? | Description |
---|---|---|---|
Quiet | /quiet | Yes | Runs the installer, displaying no UI and no prompts. |
NoRestart | /norestart | No | Suppresses any attempts to restart. By default, the UI will prompt before restart. |
Help | /help | No | Provides help and quick reference. Displays the correct use of the setup command, including a list of all options and behaviors. |
You might want line breaks to be automatically inserted within words only in the second column of a table. To limit the breaks to the second column, apply the class mx-tdCol2BreakAll
by using the div
wrapper syntax as shown earlier.
A data matrix table has both a header and a weighted first column, creating a matrix with an empty cell in the top left. Docs has custom Markdown for data matrix tables:
Every entry in the first column must be styled as bold (**bold**
); otherwise the tables won't be accessible for screen readers or valid for Docs.
HTML tables aren't recommended for docs.microsoft.com. They aren't human readable in the source - which is a key principle of Markdown.