class: center, middle, inverse, title-slide # STATS 220 ## R Markdownπ§Ά --- class: middle .pull-left[ .center[ [<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/rmarkdown.png" width="26%">](https://rmarkdown.rstudio.com) [<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/blogdown.png" width="26%">](https://bookdown.org/yihui/blogdown/) [<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/bookdown.png" width="26%">](http://bookdown.org) [<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/xaringan.png" width="26%">](http://slides.yihui.org/xaringan/) ] ] .pull-right[ ## .center[Dynamic documents] * Combine code, rendered output (such as figures), and prose * Reproduce your analyses * Collaborate and share code with others * Communicate your results with others ] ??? * graphics for communication * keep learning communication * Rmd: a tool for integrating prose, code, and results * one md file for multiple documents --- class: inverse middle center # .large[.large[.large[<i class="fab fa-r-project"></i>]]] + .large[.large[.large[.blue[<i class="fab fa-markdown"></i>]]]] ??? Rmd: glue 2 things together --- ## Markdown .blue[<i class="fab fa-markdown"></i>] `.md` * Created by [John Gruber](https://daringfireball.net/projects/markdown/) & [Aaron Swartz](http://www.aaronsw.com) in 2004 * A lightweight .brown[markup language] to add formatting elements to plain-text documents, contrasting to WYSIWYG * Portable, platform independent, everywhere (e.g. Piazza, but not Canvas π ) <img src="img/piazza-md.png" width="100%"> ??? * md originated back to 2004, but emerged around 2010 * aaron, reddit, a loss * The course website: write in md, and render in html by hugo * commonmark: it's one of markdown flavours, richer features. e.g. github * learned it in 5 mins, unlike R --- class: center middle ## .blue[<i class="fab fa-markdown"></i>] Basic syntax <img src="img/md-syntax.png"> More on [Markdown syntax](https://www.markdownguide.org/basic-syntax/) --- class: inverse background-image: url(https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-blanks/rmarkdown_wizards_blank.png) background-size: 100% 100% --- .pull-left[ ## Create R Markdown `.Rmd` <br> .center[ <img src="img/click-rmd.png" width="20%"> <img src="img/new-rmd.png" width="60%">] ] .pull-right[ ````markdown --- title: "STATS220 R Markdown demo" author: "Earo Wang" date: "`r lubridate::today()`" output: html_document --- ```{r setup, include = FALSE} library(knitr) opts_knit$set(root.dir = here::here()) opts_chunk$set(echo = TRUE) ``` ## R Markdown You can embed an R code chunk like this: ```{r gapminder, message = FALSE} library(tidyverse) gp <- read_rds("data/gapminder.rds") gp ``` See <http://rmarkdown.rstudio.com>. ```` ] --- .pull-left[ ## YAML header YAML is commonly used for configuration files, and starts and ends with `---` ````markdown `r lubridate::today()` ```` β¬οΈ inline executable R code * one markdown file, many formats + `pdf_document` + `xaringan::moon_reader` .small[ To compile to PDF, ```r install.packages('tinytex') tinytex::install_tinytex() ``` ] ] .pull-right[ ````markdown *--- *title: "STATS220 R Markdown demo" *author: "Earo Wang" *date: "`r lubridate::today()`" *output: html_document *--- ```{r setup, include = FALSE} library(knitr) opts_knit$set(root.dir = here::here()) opts_chunk$set(echo = TRUE) ``` ## R Markdown You can embed an R code chunk like this: ```{r gapminder, message = FALSE} library(tidyverse) gp <- read_rds("data/gapminder.rds") gp ``` See <http://rmarkdown.rstudio.com>. ```` ] --- .pull-left[ ## R chunk for global settings ````markdown ```{r setup, include = FALSE} library(knitr) opts_knit$set(root.dir = here::here()) opts_chunk$set(echo = TRUE) ``` ```` β¬οΈ executable R chunk * `r`: language engine * `setup`: unique chunk name * `include`: include chunk output or not * `echo`: display source code or not ] .pull-right[ ````markdown --- title: "STATS220 R Markdown demo" author: "Earo Wang" date: "`r lubridate::today()`" output: html_document --- *```{r setup, include = FALSE} *library(knitr) *opts_knit$set(root.dir = here::here()) *opts_chunk$set(echo = TRUE) *``` ## R Markdown You can embed an R code chunk like this: ```{r gapminder, message = FALSE} library(tidyverse) gp <- read_rds("data/gapminder.rds") gp ``` See <http://rmarkdown.rstudio.com>. ```` ] --- .pull-left[ ## Write in markdown <br> .center[Weave together narrative text and code] <br> <br> .center[<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/knitr.png" width="240px">] ] .pull-right[ ````markdown --- title: "STATS220 R Markdown demo" author: "Earo Wang" date: "`r lubridate::today()`" output: html_document --- ```{r setup, include = FALSE} library(knitr) opts_knit$set(root.dir = here::here()) opts_chunk$set(echo = TRUE) ``` *## R Markdown *You can embed an R code chunk like this: ```{r gapminder, message = FALSE} library(tidyverse) gp <- read_rds("data/gapminder.rds") gp ``` *See <http://rmarkdown.rstudio.com>. ```` ] --- .pull-left[ ## R chunk * `mpg`: unique chunk name * `message`: suppress message or not ```r library(tidyverse) ``` ``` #> ββ Attaching packages βββββββββββββββββββββββββββββββββββββββ tidyverse 1.3.1 ββ ``` ``` #> β ggplot2 3.3.3 β purrr 0.3.4 #> β tibble 3.1.1 β dplyr 1.0.5 #> β tidyr 1.1.3 β stringr 1.4.0 #> β readr 1.4.0 β forcats 0.5.1 ``` ``` #> ββ Conflicts ββββββββββββββββββββββββββββββββββββββββββ tidyverse_conflicts() ββ #> β dplyr::filter() masks stats::filter() #> β dplyr::lag() masks stats::lag() ``` ] .pull-right[ ````markdown --- title: "STATS220 R Markdown demo" author: "Earo Wang" date: "`r lubridate::today()`" output: html_document --- ```{r setup, include = FALSE} library(knitr) opts_knit$set(root.dir = here::here()) opts_chunk$set(echo = TRUE) ``` ## R Markdown You can embed an R code chunk like this: *```{r gapminder, message = FALSE} *library(tidyverse) *gp <- read_rds("data/gapminder.rds") *gp *``` See <http://rmarkdown.rstudio.com>. ```` ] --- .pull-left[ ## Render <br> <br> 3 ways to render an `.Rmd` 1. click <img src="img/knit.png" width="20%"> 2. shortcut: Ctrl/Cmd + Shift + K 3. `rmarkdown::render("demo.Rmd")` ] .pull-right[ <img src="img/demo-rmd-render.png", style = "box-shadow: 3px 5px 3px 1px #00000080;", width="100%"> ] --- ````r ```{r scatterplot, fig.align = "center", fig.width = 4.5, fig.height = 4} ggplot(gp, aes(gdpPercap, lifeExp)) + geom_point() + scale_x_log10() ``` ```` <img src="figure/rmd-scatterplot-1.png" width="540" style="display: block; margin: auto;" /> --- ````r ```{r scatterplot, fig.align = "center", fig.width = 4.5, fig.height = 4} ggplot(gp, aes(gdpPercap, lifeExp)) + geom_point() + scale_x_log10() ``` ```` .pull-left[ ## Chunk options * `scatterplot` gives the chunk a name. * `fig.align`: alignment of figures * `fig.width`/`fig.height` * [other figure options](https://yihui.org/knitr/options/#plots) **Good practice**: naming every single chunk! ] .pull-right[ <img src="https://itsalocke.com/blog/img/namer-oh-no.png" width="100%"> <img src="https://itsalocke.com/blog/img/namer-yaaasss.png" width="100%"> .footnote[image credit: Maelle Salmon] ] --- ````r ```{r show-code, ref.label = "scatterplot", eval = FALSE} ``` ```` .pull-left[ ## Reuse chunks by reference * `ref.label`: labels of the chunks from which the code of the current chunk is inherited * `eval`: evaluate the code chunk **Good practice**: naming every single chunk! ] .pull-right[ <img src="figure/right-plot-1.png" width="480" style="display: block; margin: auto;" /> ] --- <style type="text/css"> table { margin: auto; border-top: 1px solid #666; border-bottom: 1px solid #666; font-size: 22px; } table thead th { border-bottom: 1px solid #ddd; } th, td { padding: 5px; } thead, tfoot, tr:nth-child(even) { background: #eee; } </style> ````r ```{r lm, echo = FALSE, cache = TRUE} fit <- lm(lifeExp ~ log10(gdpPercap), data = gp) kable(coef(summary(fit))) ``` ```` .pull-left[ ## Chunk options * `echo`: overwrites the global setting * `cache`: evaluate once and skip for the future `kable()` generates a very simple HTML table. β‘οΈ ] .pull-right[ <br> <br> | | Estimate| Std. Error| t value| Pr(>|t|)| |:----------------|---------:|----------:|---------:|------------------:| |(Intercept) | -9.100889| 1.2276738| -7.413117| 0| |log10(gdpPercap) | 19.353423| 0.3425372| 56.500206| 0| ] --- ## Beyond <i class="fab fa-r-project"></i>: language engines ````py *```{python hello} # remotes::install_github("rstudio/reticulate") x = "hello, python world!" print(x.split(" ")) ``` ```` ``` #> ['hello,', 'python', 'world!'] ``` <hr> ````sh *```{sh head} head -4 data/pisa/pisa-student.csv ``` ```` ``` #> year,country,school_id,student_id,mother_educ,father_educ,gender,computer,internet,math,read,science,stu_wgt,desk,room,dishwasher,television,computer_n,car,book,wealth,escs #> 2000,ALB,1001,1,NA,NA,female,NA,no,324.35,397.87,345.66,2.16,yes,no,no,1,3+,1,11-50,-0.6,0.10575582991490981 #> 2000,ALB,1001,3,NA,NA,female,NA,no,NA,368.41,385.83,2.16,yes,yes,no,2,0,0,1-10,-1.84,-1.424044581128788 #> 2000,ALB,1001,6,NA,NA,male,NA,no,NA,294.17,327.94,2.16,yes,yes,no,2,0,0,1-10,-1.46,-1.306683855365612 ``` --- ## Beyond <i class="fab fa-r-project"></i>: language engines ````r ```{r db} library(RSQLite) db <- dbConnect(SQLite(), dbname = "data/pisa/pisa-student.db") ``` *```{sql query, connection = db, output.var = "pisa18"} SELECT * FROM pisa WHERE year = 2018 ``` ```` ``` #> # A tibble: 612,004 x 22 #> year country school_id student_id mother_educ father_educ #> <dbl> <chr> <chr> <chr> <chr> <chr> #> 1 2018 ALB 800002 800251 ISCED 3A ISCED 3A #> 2 2018 ALB 800002 800402 ISCED 2 ISCED 2 #> 3 2018 ALB 800002 801902 ISCED 2 ISCED 2 #> 4 2018 ALB 800002 803546 ISCED 2 ISCED 2 #> 5 2018 ALB 800002 804776 ISCED 2 ISCED 3A #> 6 2018 ALB 800002 804825 ISCED 2 ISCED 2 #> # β¦ with 611,998 more rows, and 16 more variables: #> # gender <chr>, computer <chr>, internet <chr>, #> # math <dbl>, read <dbl>, science <dbl>, stu_wgt <dbl>, #> # desk <chr>, room <chr>, dishwasher <chr>, #> # television <chr>, computer_n <chr>, car <chr>, #> # book <chr>, wealth <dbl>, escs <dbl> ``` --- ## Beyond <i class="fab fa-r-project"></i>: language engines ```r names(knitr::knit_engines$get()) ``` ``` #> [1] "awk" "bash" "coffee" "gawk" "groovy" "haskell" #> [7] "lein" "mysql" "node" "octave" "perl" "psql" #> [13] "Rscript" "ruby" "sas" "scala" "sed" "sh" #> [19] "stata" "zsh" "highlight" "Rcpp" "tikz" "dot" #> [25] "c" "cc" "fortran" "fortran95" "asy" "cat" #> [31] "asis" "stan" "block" "block2" "js" "css" #> [37] "sql" "go" "python" "julia" "sass" "scss" #> [43] "R" "bslib" ``` --- ## Reading .pull-left[ .center[[<img src="https://d33wubrfki0l68.cloudfront.net/b88ef926a004b0fce72b2526b0b5c4413666a4cb/24a30/cover.png" height="520px">](https://r4ds.had.co.nz)] ] .pull-right[ * [R Markdown](https://r4ds.had.co.nz/r-markdown.html) * [R Markdown formats](https://r4ds.had.co.nz/r-markdown-formats.html) * [R Markdown cheat sheet](https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf?_ga=2.253787336.159731575.1620189500-118347454.1620189500) * [R Markdown reference guide](https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf?_ga=2.253787336.159731575.1620189500-118347454.1620189500) ]