Rmarkdown::: Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

rmarkdown : : CHEAT SHEET SOURCE EDITOR

RENDERED OUTPUT file path to output document

What is rmarkdown? 1. New File Write with


.Rmd files · Develop your code and
ideas side-by-side in a single
5. Save and Render 6. Share find in document
publish to
rpubs.com,
Markdown
document. Run code as individual shinyapps.io, The syntax on the le renders as the output on the right.
chunks or as an entire document. set insert go to run code RStudio Connect
Rmd preview code code chunk(s) Plain text. Plain text.
Dynamic Documents · Knit together location chunk chunk show End a line with two spaces to End a line with two spaces to
plots, tables, and results with outline start a new paragraph. start a new paragraph.
narrative text. Render to a variety of 4. Set Output Format(s) Also end with a backslash\ Also end with a backslash
formats like HTML, PDF, MS Word, or and Options reload document to make a new line. to make a new line.
MS Powerpoint. *italics* and **bold** italics and bold
Reproducible Research · Upload, link superscript^2^/subscript~2~ superscript2/subscript2
to, or attach your report to share. ~~strikethrough~~ strikethrough
Anyone can read or run your code to 3. Write Text run all escaped: \* \_ \\ escaped: * _ \
reproduce your work. previous
modify chunks endash: --, emdash: --- endash: –, emdash: —
chunk run
options current # Header 1
Header 1
Workflow 2. Embed Code
chunk ## Header 2
... Header 2
...
11. Open a new .Rmd file in the RStudio IDE by ###### Header 6 Header 6
going to File > New File > R Markdown. • unordered list
- unordered list
22. Embed code in chunks. Run code by line, by - item 2 • item 2
chunk, or all at once. - item 2a (indent 1 tab) • item 2a (indent 1 tab)
- item 2b • item 2b
33. Write text and add tables, figures, images, and 1. ordered list 1. ordered list
citations. Format with Markdown syntax or the 2. item 2 2. item 2
RStudio Visual Markdown Editor.
44. Set output format(s) and options in the YAML
VISUAL EDITOR insert citations style options Insert Citations - item 2a (indent 1 tab)
- item 2b
<link url>
• item 2a (indent 1 tab)
• item 2b
http://www.rstudio.com/
header. Customize themes or add parameters Create citations from a bibliography file, a Zotero library,
to execute or add interactivity with Shiny. or from DOI references. [This is a link.](link url) This is a link.
[This is another link][id]. This is another link.
55. Save and render the whole document. Knit BUILD YOUR BIBLIOGRAPHY
periodically to preview your work as you write. At the end of the document:
add/edit
• Add BibTeX or CSL bibliographies to the YAML header. [id]: link url
66. Share your work! attributes
---
![Caption](image.png)
title: "My Document"
bibliography: references.bib or ![Caption][id2]
link-citations: TRUE At the end of the document:

Embed Code with knitr


--- [id2]: image.png Caption.
• If Zotero is installed locally, your main library will `verbatim code` verbatim code
automatically be available. ```
CODE CHUNKS OPTION DEFAULT EFFECTS
multiple lines multiple lines
Surround code chunks with ```{r} and ``` or use echo TRUE display code in output document • Add citations by DOI by searching "from DOI" in the
of verbatim code of verbatim code
the Insert Code Chunk button. Add a chunk label TRUE (display error messages in doc) Insert Citation dialog. ```
error FALSE FALSE (stop render when error occurs)
and/or chunk options inside the curly braces a er r. > block quotes block quotes
eval TRUE run code in chunk INSERT CITATIONS
```{r chunk-label, include=FALSE} include TRUE include chunk in doc a er running • Access the Insert Citations dialog in the Visual Editor equation: $e^{i \pi} + 1 = 0$ equation: e iπ + 1 = 0
summary(mtcars) message TRUE display code messages in document by clicking the @ symbol in the toolbar or by clicking
Insert > Citation. equation block: equation block:
``` warning TRUE display code warnings in document
"asis" (passthrough results) $$E = mc^{2}$$ E = m c2
• Add citations with markdown syntax by typing [@cite]
SET GLOBAL OPTIONS results "markup" "hide" (don't display results) or @cite. horizontal rule: horizontal rule:
"hold" (put all results below all code) ---
Set options for the entire document in the first chunk.
Insert Tables
fig.align "default" "le ", "right", or "center"
fig.alt NULL alt text for a figure | Right | Le | Default | Center | Right Le Default Center
```{r include=FALSE} |-------:|:------|-----------|:---------:|
knitr::opts_chunk$set(message = FALSE) fig.cap NULL figure caption as a character string 12 12 12 12
Output data frames as tables using | 12 | 12 | 12 | 12 |
``` fig.path "figure/" prefix for generating figure file paths kable(data, caption). 123 123 123 123
| 123 | 123 | 123 | 123 |
fig.width & 1 1 1 1
fig.height 7 plot dimensions in inches |1|1|1|1|
INLINE CODE out.width rescales output width, e.g. "75%", "300px" ```{r}
Insert `r <code>` into text sections. Code is evaluated collapse FALSE collapse all sources & output into a single block data <- faithful[1:4, ] HTML Tabsets
at render and results appear as text.
comment "##" prefix for each line of results knitr::kable(data, # Results {.tabset} Results
## Plots text
"Built with `r getRversion()`" --> "Built with 4.1.0" child NULL files(s) to knit and then include caption = "Table with kable") text Plots Tables
include or exclude a code chunk when ```
purl TRUE extracting source code with knitr::purl() ## Tables text
See more options and defaults by running str(knitr::opts_chunk$get()) Other table packages include flextable, gt, and kableExtra. more text

RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at rmarkdown.rstudio.com • rmarkdown 2.9.4 • Updated: 2021-08









ft
ft


ft












ft

ft


















ft






Set Output Formats and their Options in YAML Render

MS Word
MS PPT
HTML
PDF
Use the document's YAML header to set an output IMPORTANT OPTIONS DESCRIPTION When you render a
format and customize it with output options. anchor_sections Show section anchors on mouse hover (TRUE or FALSE) X document, rmarkdown:
--- citation_package The LaTeX package to process citations ("default", "natbib", "biblatex") X 1. Runs the code and embeds
title: "My Document" results and text into an .md
author: "Author Name" code_download Give readers an option to download the .Rmd source code (TRUE or FALSE) X
file with knitr.
output: code_folding Let readers to toggle the display of R code ("none", "hide", or "show") X
html_document: Indent format 2 characters, 2. Converts the .md file into the output format with
toc: TRUE css CSS or SCSS file to use to style document (e.g. "style.css") X Pandoc.
indent options 4 characters
--- dev Graphics device to use for figure output (e.g. "png", "pdf") X X HTML
knitr pandoc
.Rmd .md PDF
df_print Method for printing data frames ("default", "kable", "tibble", "paged") X X X X DOC
OUTPUT FORMAT CREATES
html_document .html fig_caption Should figures be rendered with captions (TRUE or FALSE) X X X X
Save, then Knit to preview the document output.
pdf_document* .pdf highlight Syntax highlighting ("tango", "pygments", "kate", "zenburn", "textmate") X X X The resulting HTML/PDF/MS Word/etc. document will
word_document Microso Word (.docx) includes File of content to place in doc ("in_header", "before_body", "a er_body") X X be created and saved in the same directory as
powerpoint_presentation Microso Powerpoint (.pptx)
the .Rmd file.
keep_md Keep the Markdown .md file generated by knitting (TRUE or FALSE) X X X X
odt_document OpenDocument Text Use rmarkdown::render() to render/knit in the R
keep_tex Keep the intermediate TEX file used to convert to PDF (TRUE or FALSE) X
rtf_document Rich Text Format console. See ?render for available options.
latex_engine LaTeX engine for producing PDF output ("pdflatex", "xelatex", or "lualatex") X
md_document Markdown
github_document
ioslides_presentation
Markdown for Github
ioslides HTML slides
reference_docx/_doc
theme
docx/pptx file containing styles to copy in the output (e.g. "file.docx", "file.pptx")
Theme options (see Bootswatch and Custom Themes below) X
X X
Share
Publish on RStudio Connect
slidy_presentation Slidy HTML slides toc Add a table of contents at start of document (TRUE or FALSE) X X X X to share R Markdown documents
beamer_presentation* Beamer slides toc_depth The lowest level of headings to add to table of contents (e.g. 2, 3) X X X X securely, schedule automatic
* Requires LaTeX, use tinytex::install_tinytex()
toc_float Float the table of contents to the le of the main document content (TRUE or FALSE) X updates, and interact with parameters in real time.
Also see flexdashboard, bookdown, distill, and blogdown.
Use ?<output format> to see all of a format's options, e.g. ?html_document rstudio.com/products/connect/

More Header Options


PARAMETERS BOOTSWATCH THEMES STYLING WITH CSS AND SCSS INTERACTIVITY
Parameterize your documents to reuse with new Customize HTML documents with Bootswatch Add CSS and SCSS to your document by adding a Turn your report into an interactive
inputs (e.g., data, values, etc.). themes from the bslib package using the theme path to a file with the css option in the YAML header. Shiny document in 4 steps:
--- output option. 1. Add runtime: shiny to the YAML header.
1. Add parameters params: ---
in the header as state: "hawaii" Use bslib::bootswatch_themes() to list available title: "My Document" 2. Call Shiny input functions to embed input objects.
sub-values of --- themes. author: "Author Name" 3. Call Shiny render functions to embed reactive
params. ```{r} --- output: output.
data <- df[, params$state] title: "Document Title" html_document:
2. Call parameters summary(data) css: "style.css" 4. Render with rmarkdown::run() or click Run
author: "Author Name"
in code using ``` output: --- Document in RStudio IDE.
params$<name>. html_document:
theme: Apply CSS styling by writing HTML tags directly or: ---
3. Set parameters bootswatch: solar • Use markdown to apply style attributes inline. output: html_document
with Knit with --- runtime: shiny
Parameters or the ---
Bracketed Span
params argument A [green]{.my-color} word. A green word. ```{r, echo = FALSE}
of render(). CUSTOM THEMES numericInput("n",
Fenced Div "How many cars?", 5)
REUSABLE TEMPLATES Customize individual HTML elements using bslib ::: {.my-color}
variables. Use ?bs_theme to see more variables. All of these words
1. Create a new package with a inst/rmarkdown/ All of these words renderTable({
templates directory. --- are green. are green. head(cars, input$n)
2. Add a folder containing template.yaml (below) output: ::: })
html_document: ```
and skeleton.Rmd (template contents).
--- theme: • Use the Visual Editor. Go to Format > Div/Span and
name: "My Template" bg: "#121212" add CSS styling directly with Edit Attributes. Also see Shiny Prerendered for better performance.
--- fg: "#E4E4E4" rmarkdown.rstudio.com/
3. Install the package to access template by going to base_font: authoring_shiny_prerendered
google: "Prompt" .my-css-tag ...
File > New R Markdown > From Template.
--- Embed a complete app into your document with
This is a div with some text in it. shiny::shinyAppDir(). More at bookdown.org/yihui/
More on bslib at pkgs.rstudio.com/bslib/.
rmarkdown/shiny-embedded.html.

RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at rmarkdown.rstudio.com • rmarkdown 2.9.4 • Updated: 2021-08












ft
ft




































ft





ft

You might also like