Rmarkdown : : CHEAT SHEET RENDERED OUTPUT File Path to Output Document SOURCE EDITOR What Is Rmarkdown? 1
Total Page:16
File Type:pdf, Size:1020Kb
rmarkdown : : CHEAT SHEET RENDERED OUTPUT file path to output document SOURCE EDITOR What is rmarkdown? 1. New File Write with 5. Save and Render 6. Share find in document .Rmd files · Develop your code and publish to Markdown ideas side-by-side in a single rpubs.com, document. Run code as individual shinyapps.io, The syntax on the lef 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 chunk ## Header 2 Workflow ... Header 2 2. Embed Code ... 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. - item 2a (indent 1 tab) • item 2a (indent 1 tab) VISUAL EDITOR insert citations style options - item 2b • item 2b 44. Set output format(s) and options in the YAML Insert Citations header. Customize themes or add parameters Create citations from a bibliography file, a Zotero library, <link url> http://www.rstudio.com/ to execute or add interactivity with Shiny. or from DOI references. [This is a link.](link url) This is a link. 55. Save and render the whole document. Knit BUILD YOUR BIBLIOGRAPHY [This is another link][id]. This is another link. periodically to preview your work as you write. At the end of the document: • Add BibTeX or CSL bibliographies to the YAML header. [id]: link url 66. Share your work! add/edit --- attributes title: "My Document" ![Caption](image.png) bibliography: references.bib or ![Caption][id2] link-citations: TRUE At the end of the document: --- [id2]: image.png Caption. Embed Code with knitr • If Zotero is installed locally, your main library will `verbatim code` verbatim code automatically be available. OPTION DEFAULT EFFECTS ``` CODE CHUNKS multiple lines echo TRUE display code in output document • Add citations by DOI by searching "from DOI" in the multiple lines Surround code chunks with ```{r} and ``` or use 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 afer r. > block quotes block quotes eval TRUE run code in chunk INSERT CITATIONS include TRUE include chunk in doc afer running • Access the Insert Citations dialog in the Visual Editor ```{r chunk-label, include=FALSE} equation: $e^{i \pi} + 1 = 0$ equation: eiπ + 1 = 0 message TRUE display code messages in document by clicking the @ symbol in the toolbar or by clicking summary(mtcars) equation block: ``` warning TRUE display code warnings in document Insert > Citation. equation block: $$E = mc^{2}$$ 2 "asis" (passthrough results) • Add citations with markdown syntax by typing [@cite] E = m c 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. fig.align "default" "lef", "right", or "center" fig.alt NULL alt text for a figure | Right | Lef | Default | Center | Right Lef Default Center ```{r include=FALSE} Insert Tables |-------:|:------|-----------|:---------:| fig.cap NULL figure caption as a character string 12 12 12 12 knitr::opts_chunk$set(message = FALSE) 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 & 7 plot dimensions in inches | 1 | 1 | 1 | 1 | 1 1 1 1 INLINE CODE fig.height 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 Set Output Formats and their Options in YAML Render IMPORTANT OPTIONS DESCRIPTION Use the document's YAML header to set an output HTML PDF MS Word MS PPT 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" code_download Give readers an option to download the .Rmd source code (TRUE or FALSE) X results and text into an .md author: "Author Name" file with knitr. output: code_folding Let readers to toggle the display of R code ("none", "hide", or "show") X html_document: 2. Converts the .md file into the output format with Indent format 2 characters, css CSS or SCSS file to use to style document (e.g. "style.css") X toc: TRUE indent options 4 characters Pandoc. --- dev Graphics device to use for figure output (e.g. "png", "pdf") X X knitr pandoc HTML .Rmd .md PDF df_print Method for printing data frames ("default", "kable", "tibble", "paged") X X X X DOC OUTPUT FORMAT CREATES fig_caption Should figures be rendered with captions (TRUE or FALSE) X X X X html_document .html 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 Microsof Word (.docx) includes File of content to place in doc ("in_header", "before_body", "afer_body") X X be created and saved in the same directory as the .Rmd file. powerpoint_presentation Microsof Powerpoint (.pptx) 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 Markdown for Github reference_docx/_doc docx/pptx file containing styles to copy in the output (e.g. "file.docx", "file.pptx") X X Share ioslides_presentation ioslides HTML slides theme Theme options (see Bootswatch and Custom Themes below) X 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 lef 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. --- themes. sub-values of author: "Author Name" 3. Call Shiny render functions to embed reactive params. ```{r} --- output: output. html_document: data <- df[, params$state] title: "Document Title" 4. Render with rmarkdown::run() or click Run 2. Call parameters summary(data) author: "Author Name" css: "style.css" in code using ``` output: --- Document in RStudio IDE.