class: center, middle, inverse, title-slide # R Markdown ### Abhijit Dasgupta ### BIOF 339 --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> --- class: inverse, center, middle # R Markdown basics --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> ## A mashup of Markdown and R --- - Markdown is a text markup format that was meant to make writing HTML easier - Convert Markdown to HTML, with simple text markup being converted to HTML code <img src="02-rmarkdown_files/figure-html/unnamed-chunk-3-1.png" width="1028" /> --- .pull-left[ Add pieces of R code Fence it inside a *code chunk* ] .pull-right[ ````markdown Looking at the *penguins* data. ``{r} library(tidyverse) library(palmerpenguins) penguins %>% group_by(species) %>% summarise(across(bill_length_mm:body_mass_g, ~mean(., na.rm=T))) ``` ```` ] --- .pull-left[ Add pieces of R code Fence it inside a *code chunk* Add options ] .pull-right[ ````markdown Looking at the *penguins* data. ``{r, echo=FALSE, message=FALSE, warning=FALSE} library(tidyverse) library(palmerpenguins) penguins %>% group_by(species) %>% summarise(across(bill_length_mm:body_mass_g, ~mean(., na.rm=T))) ``` ```` ] --- .pull-left[ Add pieces of R code Fence it inside a *code chunk* Add options Name the chunk ] .pull-right[ ````markdown Looking at the *penguins* data. ``{r means, echo=FALSE, message=FALSE, warning=FALSE} library(tidyverse) library(palmerpenguins) penguins %>% group_by(species) %>% summarise(across(bill_length_mm:body_mass_g, ~mean(., na.rm=T))) ``` ```` ] --- You can set the chunk options for the entire document at the beginning of the document. ````markdown ``{r setup, include=FALSE} knitr::opts_chunk()$set(echo = FALSE, eval = TRUE, message=FALSE, warning=FALSE, comment = "") ``` ```` The option `include=FALSE` is equivalent to .acidinline[`echo = FALSE, eval = TRUE, results = 'hide'`] .footnote[Details of these options are [here](https://yihui.org/knitr/options/#chunk-options)] --- .pull-left[ The **rmarkdown** package leverages the [pandoc](https://pandoc.org) universal document converter to allow a R Markdown document to be converted to several different formats, as we'll see later. To specify this, and some metadata, R Markdown uses a YAML header at the very beginning of the R Markdown document The YAML section begins and ends with three dashes .footnote[YAML is another kind of structured text format.] ] .pull-right[ ```markdown --- title: "My report" author: "Abhijit" date: 'October 03, 2021' output: html_document --- ``` ] --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> --- --- class: inverse, center, middle # Rich R Markdown Documents --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> ## What can you create from R Markdown? --- + Documents - HTML - Microsoft Word - PDF (requires [LaTeX](http://www.latex-project.org)) + Presentations - HTML (`ioslides`, `revealjs`, `xaringan`) - PDF (`beamer`) - PowerPoint --- + Interactive documents - The [`htmlwidgets`](https://www.htmlwidgets.org/) meta-package + Dashboards - The [`flexdashboard`](https://rmarkdown.rstudio.com/flexdashboard/) package + Books - The [`bookdown`](https://bookdown.org/) package + Websites & Blogs - [R Markdown](https://rmarkdown.rstudio.com/lesson-13.html) - [`blogdown`](https://bookdown.org/yihui/blogdown/) package --- + Resumes/CVs - The [`vitae`](https://pkg.mitchelloharawild.com/vitae/) package + Research papers - include citations - include appropriate formatting - probably need [LaTeX](http://www.latex-project.org) -- ### See the [RMarkdown gallery](https://rmarkdown.rstudio.com/gallery.html) --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> --- The basic differences are in the front-matter at the top of your RMarkdown document ### HTML document ``` --- title: "Lectures" date: "Fall 2018" output: html_document --- ``` -- ### Word document ``` --- title: "Lectures" date: "Fall 2018" output: word_document --- ``` --- ## Presentations .pull-left[ ### ioslides ``` --- title: "Lecture 2: \nData Frame, Matrix, List" author: "Abhijit Dasgupta" date: "September 19, 2018" output: ioslides_presentation --- ``` ### revealjs ``` --- title: "Lecture 2: \nData Frame, Matrix, List" author: "Abhijit Dasgupta" date: "September 19, 2018" output: revealjs::revealjs_presentation: theme: default highlight: default transition: fade slide_level: 1 --- ``` ] .pull-right[ ### Slides delimited by markdown sections ``` # Slide 1 This is my first slide # Slide 2 This is my second slide ``` ] --- ## Presentations .pull-left[ ### Powerpoint ``` --- title: "Lecture 2: \nData Frame, Matrix, List" author: "Abhijit Dasgupta" date: "September 19, 2018" output: powerpoint_presentation --- ``` ] .pull-right[ ### Slides delimited by markdown sections ``` # Slide 1 This is my first slide # Slide 2 This is my second slide ``` ] --- ## Presentations .pull-left[ ### xaringan ``` --- title: "Lecture 2: \nData Frame, Matrix, List" author: "Abhijit Dasgupta" date: "September 19, 2018" output: xaringan::moon_reader: css: [default, './robot.css', './robot-fonts.css'] #css: [default, metropolis, metropolis-fonts] nature: ratio: '16:9' highlightLanguage: R countIncrementalSlides: false highlightStyle: zenburn highlightLines: true --- ``` ] .pull-right[ ### Slides delimited by `---` ``` --- # Slide 1 This is my first slide --- # Slide 2 This is my second slide ``` ] --- ## R Markdown Templates .pull-left[ - Several packages provide RMarkdown templates - You can include citations - EndNote, MEDLINE, RIS, BibTeX formats for references - See <a href="https://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html">https://rmarkdown.rstudio.com/ authoring_bibliographies_and_citations.html</a> ] .pull-right[ <img src="../img/templates.png" width="708" /> ] --- ## You can even use other coding languages in R Markdown!! The **knitr** package allows for [many](https://bookdown.org/yihui/rmarkdown/language-engines.html) other computer languages to be run within a R Markdown document. Of particular note are Python and Julia, which can be interfaced from R using the **reticulate** and **JuliaCall** packages, respectively .left-column30[ This is an example of a Python manual written using R Markdown ] .right-column30[ <a href="https://www.araastat.com/BIOF085" target="_blank"> <img src="02-rmarkdown_files/figure-html/unnamed-chunk-7-1.png" width="2560" /> </a> ] --- class: middle, center, inverse # Creating documents from R scripts --- layout: true <div class="my-header"> <span>BIOF 339: Practical R</span> </div> ## Creating documents from R scripts --- A typical R workflow is to do your analyses using R scripts (`.R`) and then, at the end, copy-and-paste code to create a R Markdown file for reporting. .acidinline[This is actually not necessary] You can actually create R Markdown-based documents .heatinline[directly from R scripts] This is done using `rmarkdown::render` applied to a R script formatted in a particular way, as we will see. This can also be done in RStudio using the shortcut Ctrl/Cmd-Shift-K or the small binder icon. --- .pull-left[ ```r library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) ``` ] .pull-right[ ``` species n percent Adelie 152 0.4418605 Chinstrap 68 0.1976744 Gentoo 124 0.3604651 ``` ] --- .pull-left[ ```r #' # Exploring the penguins dataset #' #' The following is relative frequencies of the different species of penguins library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) ``` You write lines starting with `#'`, i.e, hash and then single-quote These are interpreted as Markdown lines ] .pull-right[ <img src="02-rmarkdown_files/figure-html/unnamed-chunk-11-1.png" width="533" /> ] --- .pull-left[ ```r #' # Exploring the penguins dataset #' #' The following is relative frequencies of the different species of penguins *#+ message=FALSE, warning=FALSE library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) ``` You can then add code chunk options by starting a line with `#+`. ] .pull-right[ <img src="02-rmarkdown_files/figure-html/unnamed-chunk-13-1.png" width="533" /> ] --- .pull-left[ ```r #' # Exploring the penguins dataset #' #' The following is relative frequencies of the different species of penguins #+ message=FALSE, warning=FALSE library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) %>% adorn_pct_formatting() %>% * knitr::kable() ``` Format the output for HTML ] .pull-right[ <img src="02-rmarkdown_files/figure-html/unnamed-chunk-15-1.png" width="533" /> ] --- .pull-left[ ```r #' --- #' title: Exploring penguins #' author: Abhijit #' date: "`r format(Sys.Date(), '%B %d, %Y')`" #' output: #' html_document: #' theme: cerulean #' --- #' #' # Exploring the penguins dataset #' #' The following is relative frequencies of the different species of penguins #+ message=FALSE, warning=FALSE library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) %>% adorn_pct_formatting() %>% knitr::kable() ``` Add header information, starting lines with `#'`, since these lines need to be text in the corresponding Rmd file ] .pull-right[ <img src="02-rmarkdown_files/figure-html/unnamed-chunk-17-1.png" width="533" /> ] --- .pull-left[ ```r #' --- #' title: Exploring penguins #' author: Abhijit #' date: "`r format(Sys.Date(), '%B %d, %Y')`" #' output: #' html_document: #' theme: cerulean #' --- #' #' # Exploring the penguins dataset #' #' The following is relative frequencies of the different species of penguins *#+ message=FALSE, warning=FALSE, echo=FALSE library(palmerpenguins) library(tidyverse) library(janitor) tabyl(penguins, species) %>% adorn_pct_formatting() %>% knitr::kable() ``` Remove code to clean up HTML ] .pull-right[ <img src="02-rmarkdown_files/figure-html/unnamed-chunk-19-1.png" width="533" /> ] --- ## Resources <table> <tr> <td rowspan=2> <a href="https://bookdown.org/yihui/rmarkdown" target='_blank'> <img src="02-rmarkdown_files/figure-html/10-BestPractice-25-1.png" width="267" /> </a> </td> <td> <a href = "https://bookdown.org/yihui/bookdown" target='_blank'> <img src="02-rmarkdown_files/figure-html/10-BestPractice-26-1.png" width="133" /> </a></td> </tr> <tr> <td> <a href = "https://bookdown.org/yihui/blogdown" target='_blank'> <img src="02-rmarkdown_files/figure-html/10-BestPractice-27-1.png" width="133" /> </a> </td> </tr></table>