R Programming
Total Page:16
File Type:pdf, Size:1020Kb
R programming prof. UAM dr hab. Tomasz Górecki [email protected] Department of Mathematical Statistics and Data Analysis Faculty of Mathematics and Computer Science Adam Mickiewicz University in Poznań . Tomasz Górecki (UAM) R programming 1 / 13 R Markdown – idea R Markdown provides an authoring framework for data science. You can use a single R Markdown file to both save and execute code, and generate high quality reports that can be shared with an audience. R Markdown was designed for easier reproducibility, since both the computing code and narratives are in the same document, and results are automatically generated from the source code. R Markdown supports dozens of static and dynamic/interactive output formats. Tomasz Górecki (UAM) R programming 2 / 13 R Markdown – materials Video https://vimeo.com/178485416 – short introduction to R Markdown. https://youtu.be/YXSp6VfZaUk – creating R Markdown file https://youtu.be/DNS7i2m4sB0 – R Markdown with RStudio. https://youtu.be/hODWGzpiCV0 – R Markdown tutorial. Books https://bookdown.org/yihui/rmarkdown/ – R Markdown: The Definitive Guide. https://github.com/rstudio/bookdown – Bookdown: Authoring Books and Technical Documents with R Markdown. Tomasz Górecki (UAM) R programming 3 / 13 Markdown Markdown is a simple formatting language designed to make authoring content easy for everyone. Rather than write in complex markup code (e.g. HTML or LaTeX), you write in plain text with formatting cues. pandoc uses these cues to turn your document into attractive output. Tomasz Górecki (UAM) R programming 4 / 13 R Markdown – create document Creating documents with R Markdown starts with an .Rmd file that contains a combination of markdown (content with simple text formatting) and R code chunks. The .Rmd file is fed to knitr, which executes all of the R code chunks and creates a new markdown (.md) document which includes the R code and it’s output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating a finished web page, PDF, MS Word document or other format. This may sound complicated, but R Markdown makes it extremely simple by encapsulating all of the above processing into a single render function. Tomasz Górecki (UAM) R programming 5 / 13 R Markdown – output formats You can create many types of output from a single .Rmd file. R Markdown has built in support for HTML, PDF, MS Word, ODT, RTF, Markdown, and Github flavored Markdown documents; Beamer, slidy.js, ioslides, and reveal.js slideshows; and websites, dashboards, books, handouts, package vignettes, and Shiny apps. Tomasz Górecki (UAM) R programming 6 / 13 R Markdown – structure of document There are three basic components of an R Markdown document: metadata, text, code. The metadata is written between the pair of three dashes —. The syntax for the metadata is YAML, so sometimes it is also called the YAML metadata or the YAML frontmatter. The body of a document follows the metadata. The syntax for text is Markdown. There are two types of computer code: A code chunk starts with three backticks like ```r where r indicates the language name, and ends with three backticks. An inline R code expression starts with `r and ends with a backtick `. Tomasz Górecki (UAM) R programming 7 / 13 R code chunks Within an R Markdown file, R code chunks can be embedded with the native Markdown syntax for fenced code regions. Tomasz Górecki (UAM) R programming 8 / 13 Inline R code You can also evaluate R expressions inline by enclosing the expression within a single back-tick qualified with ’r’. Tomasz Górecki (UAM) R programming 9 / 13 Formatting text header 1: # header 1 header 2: ## header 2 header 3: ### header 3 header 4: #### header 4 bold: **text** italics: *text* link: [link](text) Image: Logo ![Logo](img/logo.png) LaTeX: $A = \pi \times r^{2}$ . Tomasz Górecki (UAM) R programming 10 / 13 Selected instructions for code chunks eval – Is the code run and the results included in the output? include – Are the code and the results included in the output (the code is still run)? echo – Is the code displayed alongside the results. warning – Are warning messages displayed. error – Are error messages displayed. message – Are messages displayed. cache – Are the results cached for future renders. dev – format of generated images (default PNG). fig.width – Width (in inches) of the plots. fig.height – Height (in inches) of the plots. fig.align – Align of the plots: “left”, “right”, “center”. Tomasz Górecki (UAM) R programming 11 / 13 R Markdown – cheatsheet Pandoc’s Markdown Set render options with YAML Write with syntax on the le! to create e"ect on right (a!er render) When you render, R Markdown rmarkdown Plain text 1. runs the R code, embeds results and text into .md file with knitr End a line with two spaces to start a new paragraph. 2. then converts the .md file into the finished format with pandoc *italics* and **bold** `verbatim code` sub/superscript^2^~2~ sub-option description ~~strikethrough~~ html pdf word odt rtf md gituhb ioslides slidy beamer escaped: \* \_ \\ citation_package The LaTeX package to process citations, natbib, biblatex or none X X X endash: --, emdash: --- equation: $A = \pi*r^{2}$ Set a document’s code_folding Let readers to toggle the display of R code, "none", "hide", or "show" X default output format --- equation block: output: html_document colortheme Beamer color theme to use X in the YAML header: --- $$E = mc^{2}$$ # Body css CSS file to use to style document X X X > block quote dev Graphics device to use for figure output (e.g. "png") X X X X X X X duration Add a countdown timer (in minutes) to footer of slides X # Header1 {#anchor} output value creates fig_caption Should figures be rendered with captions? X X X X X X X ## Header 2 {#css_id} html_document html fig_height, fig_width Default figure height and width (in inches) for document X X X X X X X X X X ### Header 3 {.css_class} pdf_document pdf (requires Tex ) word_document Microso! Word (.docx) highlight Syntax highlighting: "tango", "pygments", "kate","zenburn", "textmate" X X X X X #### Header 4 odt_document OpenDocument Text includes File of content to place in document (in_header, before_body, a!er_body) X X X X X X X X ##### Header 5 rtf_document Rich Text Format incremental Should bullets appear one at a time (on presenter mouse clicks)? X X X ###### Header 6 md_document Markdown keep_md Save a copy of .md file that contains knitr output X X X X X X <!--Text comment--> github_document Github compatible markdown keep_tex Save a copy of .tex file that contains knitr output X X ioslides_presentation ioslides HTML slides latex_engine Engine to render latex, "pdflatex", "xelatex", or "lualatex" X X \textbf{Tex ignored in HTML} <em>HTML ignored in pdfs</em> slidy_presentation slidy HTML slides lib_dir Directory of dependency files to use (Bootstrap, MathJax, etc.) X X X <http://www.rstudio.com> beamer_presentation Beamer pdf slides (requires Tex) mathjax Set to local or a URL to use a local/URL version of MathJax to render equations X X X [link](www.rstudio.com) Jump to [Header 1](#anchor) Indent 2 Indent 4 md_extensions Markdown extensions to add to default definition or R Markdown X X X X X X X X X X spaces spaces image: Customize output with --- number_sections Add section numbering to headers X X sub-options (listed to output: html_document: ![Caption](smallorb.png) the right): code_folding: hide pandoc_args Additional arguments to pass to Pandoc X X X X X X X X X X * unordered list toc_float: TRUE preserve_yaml Preserve YAML front matter in final document? X + sub-item 1 --- + sub-item 2 # Body reference_docx docx file whose styles should be copied when producing docx output X - sub-sub-item 1 self_contained Embed dependencies into the doc X X X * item 2 html tabsets slide_level The lowest heading level that defines individual slides X Continued (indent 4 spaces) Use tablet css class to place sub-headers into tabs smaller Use the smaller font size in the presentation? X 1. ordered list # Tabset {.tabset .tabset-fade .tabset-pills} smart Convert straight quotes to curly, dashes to em-dashes, … to ellipses, etc. X X X 2. item 2 ## Tab 1 template Pandoc template to use when rendering file quarterly_report.html). X X X X X i) sub-item 1 A. sub-sub-item 1 text 1 Tabset theme Bootswatch or Beamer theme to use for page X X (@) A list whose numbering ## Tab 2 Tab 1 Tab 2 toc Add a table of contents at start of document X X X X X X X text 2 continues a!er text 1 toc_depth The lowest level of headings to add to table of contents X X X X X X ### End tabset End tabset toc_float Float the table of contents to the le! of the main content X (@) an interruption Term 1 : Definition 1 Create a Reusable Template Table Suggestions Citations and Bibliographies | Right | Le! | Default | Center | 1. Create a new package with a inst/rmarkdown/templates Several functions format R data into tables Create citations with .bib, .bibtex, .copac, .enl, .json, |------:|:-----|---------|:------:| directory .medline, .mods, .ris, .wos, and .xml files | 12 | 12 | 12 | 12 | | 123 | 123 | 123 | 123 | 2. In the directory, Place a folder that contains: --- | 1 | 1 | 1 | 1 | template.yaml (see below) 1. Set bibliography file and CSL 1.0 bibliography: refs.bib skeleton.Rmd (contents of the template) Style file (optional) in the YAML header csl: style.csl - slide bullet 1 any supporting files - slide bullet 2 2. Use citation keys in text --- 3. Install the package (>- to have bullets appear on click) 4. Access template in wizard at File ▶ New File ▶ R Markdown data <- faithful[1:4, ] Smith cited [@smith04].