Rmd: glue 2 things together
.md
.Rmd
---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 MarkdownYou 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>.
YAML is commonly used for configuration files, and starts and ends with
---
`r lubridate::today()`
β¬οΈ inline executable R code
pdf_document
xaringan::moon_reader
To compile to PDF,
install.packages('tinytex')tinytex::install_tinytex()
---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 MarkdownYou 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>.
```{r setup, include = FALSE}library(knitr)opts_knit$set(root.dir = here::here())opts_chunk$set(echo = TRUE)```
β¬οΈ executable R chunk
r
: language enginesetup
: unique chunk nameinclude
: include chunk output or notecho
: display source code or not---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 MarkdownYou 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>.
Weave together narrative text and code
---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 MarkdownYou 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>.
mpg
: unique chunk namemessage
: suppress message or notlibrary(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()
---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 MarkdownYou 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>.
3 ways to render an .Rmd
rmarkdown::render("demo.Rmd")
```{r scatterplot, fig.align = "center", fig.width = 4.5, fig.height = 4}ggplot(gp, aes(gdpPercap, lifeExp)) + geom_point() + scale_x_log10()```
```{r scatterplot, fig.align = "center", fig.width = 4.5, fig.height = 4}ggplot(gp, aes(gdpPercap, lifeExp)) + geom_point() + scale_x_log10()```
scatterplot
gives the chunk a name.fig.align
: alignment of figuresfig.width
/fig.height
Good practice: naming every single chunk!
image credit: Maelle Salmon
```{r show-code, ref.label = "scatterplot", eval = FALSE}```
ref.label
: labels of the chunks from which the code of the current chunk is inheritedeval
: evaluate the code chunkGood practice: naming every single chunk!
```{r lm, echo = FALSE, cache = TRUE}fit <- lm(lifeExp ~ log10(gdpPercap), data = gp)kable(coef(summary(fit)))```
echo
: overwrites the global settingcache
: evaluate once and skip for the futurekable()
generates a very simple HTML table. β‘οΈ
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | -9.100889 | 1.2276738 | -7.413117 | 0 |
log10(gdpPercap) | 19.353423 | 0.3425372 | 56.500206 | 0 |
```{python hello}# remotes::install_github("rstudio/reticulate")x = "hello, python world!"print(x.split(" "))```
#> ['hello,', 'python', 'world!']
```{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
```{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>
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"
Keyboard shortcuts
β, β, Pg Up, k | Go to previous slide |
β, β, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |