class: bg-pro center middle hide-slide-number <div class="bg-black white" style="width:45%;right:0;bottom:0;padding-left:5px;border: solid 4px white;margin: auto;"> <i class="fas fa-exclamation-circle"></i> These slides are viewed best by Chrome and occasionally need to be refreshed if elements did not load properly. See here for <a href=session4.pdf>PDF <i class="fas fa-file-pdf"></i></a>. </div> --- count: false background-image: url(images/bg1.jpg) background-size: cover class: hide-slide-number title-slide :::::::::: { .grid grid: 50% 50% / 1fr;} ::: item center # <span style="text-shadow: 2px 2px 30px white;">R Markdown Workshop</span> <br><br> ## <span style="color:yellow;text-shadow: 2px 2px 30px black;">Reproducible Reports with R Markdown</span> ::: ::: center shade_black animated bounceInUp slower Presented by Emi Tanaka School of Mathematics and Statistics <img src="assets/USydLogo-white.svg" style="width:200px"><br>
<i class="fas fa-envelope faa-float animated "></i>
dr.emi.tanaka@gmail.com
<i class="fab fa-twitter faa-float animated faa-fast "></i>
@statsgen .bottom_abs.width100.bg-black[ 1st Dec 2019 @ Biometrics by the Botanic Gardens | Adelaide, Australia ] ::: :::::::::: --- class: center middle .font_large[🔎 Open and inspect the file] .font_large[`demo-header.Rmd`] --- # Cross Reference ::: grid ::: item * When you make a header via Rmd ```markdown # Some Header ``` an .red[id is created automatically]. * The id is created by replacing .blue[space with `-`] and making it .blue[all lower case]. * Now you can link to this header by `[some text](#some-header)`. * Cross references work for both pdf and html outputs. ::: ::: item <center> <iframe src="exercises/demo-header.html" width = "90%", height = "500px"></iframe> </center> ::: ::: --- # Direct Reference for `html` <br> ::: paddings * For `html` output, you can also give a link directly to the relevant section * E.g. open `demo-header.html` in a web browser * Append say `#chicken-data` to the url. It should look like .center[ `demo-header.html#chicken-data` ] * It should have taken you to straight to the corresponding header 🏃 ::: --- # User-defined id <br> ::: paddings * You can define your own id by appending `{#your-id}`. ```markdown # Some header {#header1} ``` * Now you can link to this header with the id `header1`. * Note there should be no space in the id name! ::: --- class: center middle .font_large[🔍 Open and inspect the file] .font_large[`demo.bib`] --- class: font_smaller # Bibliography * BibTeX citation style format is used to store references in `.bib` files. * Remember that you can get most BibTeX citation for R packages `citation` function. (Scroll below to see the BibTeX citation). .scroll-350[ ```r citation("xaringan") ``` ``` To cite package 'xaringan' in publications use: Yihui Xie (2019). xaringan: Presentation Ninja. R package version 0.9. https://CRAN.R-project.org/package=xaringan A BibTeX entry for LaTeX users is @Manual{, title = {xaringan: Presentation Ninja}, author = {Yihui Xie}, year = {2019}, note = {R package version 0.9}, url = {https://CRAN.R-project.org/package=xaringan}, } ``` ] --- class: center middle .font_large[🔍 Open, inspect and knit the file] .font_large[`demo-citation.Rmd`] --- # Citations <br> ::: paddings * You can include BibTeX by specifying the `bib` file at YAML as: ```markdown bibliography: bibliography.bib ``` [@bibtex-key] <i class="fas fa-arrow-right"></i> (Author et al. 2019) or @bibtex-key <i class="fas fa-arrow-right"></i> Author et al. 2019 * See `demo-citation.Rmd` ::: --- class: font_small # Figure References ::: paddings * Support for figure references are included for output format type `bookdown::pdf_document2` for pdf or `bookdown::html_document2` for html. ````markdown ```{r plot1, fig.cap = "Caption"} ggplot(cars, aes(dist, speed)) + geom_point() ``` ```` * Above figure number can be referenced as .red[`\@ref(fig:plot1)`] * The reference label has the prefix `fig:` before the chunk label. ::: --- class: font_small # Table References ::: paddings * Support for table references are also included for output format type `bookdown::pdf_document2` for pdf or `bookdown::html_document2` for html. ````markdown ```{r table1} knitr::kable(cars, booktabs = TRUE, caption = "Caption") ``` ```` * Above table number can be referenced as .red[`\@ref(tab:table1)`] * The reference label has the prefix `tab:` before the chunk label. ::: --- class: font_small # Markdown for Captions ::: paddings ````markdown ```{r plot1, fig.cap = "(ref:label)"} ggplot(cars, aes(dist, speed)) + geom_point() ``` ```` * Then the caption can be entered in a separate paragraph with empty lines above and below it ```markdown (ref:label) This is the *caption* with **markdown**. ``` * You can substitute `label` with another unique label composed of alphanumeric characters, :, -, or / * This caption supports markdown syntax * This is great for long captions * It also works for tables! ::: --- class: center middle .font_large[🔧 Open and work through] .font_large[`challenge-12-references.Rmd`]
05
:
00
--- class: font_small # Parametrized Report ````markdown --- title: "Parameterized Report" params: * species: setosa output: html_document --- ```{r, message = FALSE, fig.dim = c(3,2)} library(tidyverse) iris %>% * filter(Species==params$species) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(aes(color=Species)) ``` ```` ::: {.output .pos top:60px; right:10px;} <img src = "images/parameterised-report-eg1.png" width = "550px"/> ::: --- class: font_smaller # Knit with Parameters ::: grid ::: item ```markdown --- title: "Parameterized Report" params: species: label: "Species" value: setosa input: select choices: [setosa, versicolor, virginica] color: red max: label: "Maximum Sepal Width" value: 4 input: slider min: 4 max: 5 step: 0.1 output: html_document --- ``` ::: ::: item <img src = "images/knit-with-params.png" width = "250px" style="border: solid 3px black;"/> ````markdown ```{r, message = params$printmsg} library(tidyverse) iris %>% filter(Species==params$species) %>% filter(Sepal.Width < params$max) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(color = params$color) + labs(title = params$species) ``` ```` ::: ::: --- class: font_smaller # Shiny Report Generator ::: grid ::: item ```markdown --- title: "Parameterized Report" params: species: label: "Species" value: setosa input: select choices: [setosa, versicolor, virginica] color: red max: label: "Maximum Sepal Width" value: 5 input: slider min: 4 max: 5 step: 0.05 output: html_document --- ``` ::: ::: item <Br> <img src = "images/shiny-params.png" width = "100%" style="border: solid 3px black;"/> ::: ::: --- class: center middle .font_large[🔧 Open and work through] .font_large[`challenge-13-params.Rmd`]
05
:
00
--- class: font_smaller # R Markdown via Command Line ::: grid ::: item `demo-render.Rmd` ````markdown --- title: "Parameterized Report" params: * species: setosa output: html_document --- ```{r, message = FALSE, fig.dim = c(3,2)} library(tidyverse) iris %>% * filter(Species==params$species) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(aes(color=Species)) ``` ```` ::: ::: item You can knit this file via R command by<br> using the `render` function: ```r library(rmarkdown) render("demo-render.Rmd") ``` <br> You can overwrite the YAML values<br> by supplying arguments to `render`: ```r library(rmarkdown) render("demo-render.Rmd", output_format = "pdf_document", params = list(species = "virginica")) ``` ::: ::: --- class: center middle .font_large[🔧 Open and work through] .font_large[`challenge-14-letters.Rmd`]
10
:
00
--- # Themes: `html_document` <!-- The Modal --> <div id="myModal" class="modal"> <div id="caption"></div> <span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span> <iframe class="modal-content" id="img01"></iframe><br> </div> You can change the look of the html document by specifying themes: ::: grid ::: item * `default` <img class="modalImg" alt="default"/> * `cerulean` <img class="modalImg" alt="cerulean"/> * `journal` <img class="modalImg" alt="journal"/> * `flatly` <img class="modalImg" alt="flatly"/> * `darkly` <img class="modalImg" alt="darkly"/> * `readable` <img class="modalImg" alt="readable"/> * `spacelab` <img class="modalImg" alt="spacelab"/> * `united` <img class="modalImg" alt="united"/> ::: ::: item * `cosmo` <img class="modalImg" alt="cosmo"/> * `lumen` <img class="modalImg" alt="lumen"/> * `paper` <img class="modalImg" alt="paper"/> * `sandstone` <img class="modalImg" alt="sandstone"/> * `simplex` <img class="modalImg" alt="simplex"/> * `yeti` <img class="modalImg" alt="yeti"/> * `NULL` <img class="modalImg" alt="null"/> ::: ::: item ```markdown output: html_document: theme: cerulean ``` These [bootswatch](https://bootswatch.com/) themes attach the whole bootstrap library which makes your html file size larger. ::: ::: --- # `prettydoc` <!-- The Modal --> <div id="myModal2" class="modal"> <div id="caption2"></div> <span class="close" onclick="document.getElementById('myModal2').style.display='none'">×</span> <iframe class="modal-content" id="img02"></iframe><br> </div> ::: grid ::: item `prettydoc` 📦 is a community contributed `theme` that is light-weight: * `cayman` <img class="modalImg2" alt="cayman"/> * `tactile` <img class="modalImg2" alt="tactile"/> * `architect` <img class="modalImg2" alt="architect"/> * `leonids` <img class="modalImg2" alt="leonids"/> * `hpstr` <img class="modalImg2" alt="hpstr"/> ::: ::: item ```markdown output: prettydoc::html_pretty: theme: cayman ``` See more about it below: https://prettydoc.statr.me/ ::: ::: --- # `rmdformats` <!-- The Modal --> <div id="myModal3" class="modal"> <div id="caption3"></div> <span class="close" onclick="document.getElementById('myModal3').style.display='none'">×</span> <iframe class="modal-content" id="img03"></iframe><br> </div> ::: grid ::: item `rmdformats` 📦 contains four built-in `html` formats: * `readthedown` <img class="modalImg3" alt="readthedown"/> * `html_clean` <img class="modalImg3" alt="html_clean"/> * `html_docco` <img class="modalImg3" alt="html_docco"/> * `material` <img class="modalImg3" alt="material"/> ::: ::: item You can use these formats by simply specifying the output in YAML as below: ```markdown output: rmdformats::readthedown ``` See more about it below: https://github.com/juba/rmdformats ::: ::: --- # `rticles` - LaTeX Journal Article Templates <!-- The Modal --> <div id="myModal4" class="modal"> <div id="caption4"></div> <span class="close" onclick="document.getElementById('myModal4').style.display='none'">×</span> <iframe class="modal-content" id="img04"></iframe><br> </div> ::: grid ::: item * `acm` <img class='modalPdf' alt='acm_article'/> * `acs` <img class='modalPdf' alt='acs_article'/> * `aea` <img class='modalPdf' alt='aea_article'/> * `agu` <img class='modalPdf' alt='agu_article'/> * `amq` <img class='modalPdf' alt='amq_article'/> * `ams` <img class='modalPdf' alt='ams_article'/> * `asa` <img class='modalPdf' alt='asa_article'/> * `biometrics` <img class='modalPdf' alt='biometrics_article'/> * `copernicus` <img class='modalPdf' alt='copernicus_article'/> ::: ::: item * `elsevier` <img class='modalPdf' alt='elsevier_article'/> * `frontiers` <img class='modalPdf' alt='frontiers_article'/> * `ieee` <img class='modalPdf' alt='ieee_article'/> * `jss` <img class='modalPdf' alt='jss_article'/> * `mdpi` <img class='modalPdf' alt='mdpi_article'/> * `mnras` <img class='modalPdf' alt='mnras_article'/> * `peerj` <img class='modalPdf' alt='peerj_article'/> * `plos` <img class='modalPdf' alt='plos_article'/> ::: ::: item * `pnas` <img class='modalPdf' alt='pnas_article'/> * `rjournal` <img class='modalPdf' alt='rjournal_article'/> * `rsos` <img class='modalPdf' alt='rsos_article'/> * `rss` <img class='modalPdf' alt='rss_article'/> * `sage` <img class='modalPdf' alt='sage_article'/> * `sim` <img class='modalPdf' alt='sim_article'/> * `springer` <img class='modalPdf' alt='springer_article'/> * `tf` <img class='modalPdf' alt='tf_article'/> :::: :::: ::: {.bottom_abs .width100 .bg-indigo .white padding-left:10px;} Go to RStudio > File > New File > R Markdown ... > From Template ::: --- # External Files in Templating ::: grid ::: item * When using `rticles`, each journal usually require external files (e.g. `cls` or image files). * These external components are stored within the package. * So use `draft` instead of `render`! ::: ::: item bg-gray **GUI** * `RStudio > File > New File > R Markdown ... > From Template` **Command line** ```r rmarkdown::draft("file.Rmd", template = "biometrics_article", package = "rticles") ``` ::: ::: --- # Making your own R Markdown template ::: paddings * You need to make an R package first!<br> Go to RStudio > New Project > New Directory > R Package or `usethis::create_package()` * When you are in your R package project, ```r usethis::use_rmarkdown_template("<Name>") ``` * Modify the `skeleton/skeleton.Rmd` to how you want and add all external files to the `skeleton` folder. * Install your package. * 🎉 And now find it at RStudio > File > New File > R Markdown > From Template. ::: --- class: center middle .font_large[🔧 Create your own <br>R Markdown Template Package!] --- background-color: #e5e5e5 # Session Information :::: scroll-350 ```r devtools::session_info() ``` ``` ─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── setting value version R version 3.6.0 (2019-04-26) os macOS Mojave 10.14.6 system x86_64, darwin15.6.0 ui X11 language (EN) collate en_AU.UTF-8 ctype en_AU.UTF-8 tz Australia/Adelaide date 2019-12-03 ─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── package * version date lib source anicon 0.1.0 2019-05-28 [1] Github (emitanaka/anicon@377aece) assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.0) broom 0.5.2 2019-04-07 [1] CRAN (R 3.6.0) callr 3.3.1 2019-07-18 [1] CRAN (R 3.6.0) cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.0) cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.0) colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0) countdown 0.2.0 2019-05-30 [1] Github (gadenbuie/countdown@c8e8710) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) devtools 2.0.2 2019-04-08 [1] CRAN (R 3.6.0) digest 0.6.22 2019-10-21 [1] CRAN (R 3.6.0) dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.0) emo 0.0.0.9000 2019-06-03 [1] Github (hadley/emo@02a5206) evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.6.0) fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0) generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0) ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.0) glue 1.3.1.9000 2019-10-24 [1] Github (tidyverse/glue@71eeddf) gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) haven 2.1.0 2019-02-19 [1] CRAN (R 3.6.0) hms 0.5.1 2019-08-23 [1] CRAN (R 3.6.0) htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0) httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.0) icon 0.1.0 2019-05-28 [1] Github (ropenscilabs/icon@a510f88) jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0) knitr 1.25 2019-09-18 [1] CRAN (R 3.6.0) lattice 0.20-38 2018-11-04 [1] CRAN (R 3.6.0) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.0) lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0) magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) modelr 0.1.4 2019-02-18 [1] CRAN (R 3.6.0) munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) nlme 3.1-140 2019-05-12 [1] CRAN (R 3.6.0) pillar 1.4.2 2019-06-29 [1] CRAN (R 3.6.0) pkgbuild 1.0.3 2019-03-20 [1] CRAN (R 3.6.0) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0) pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0) prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0) processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0) ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0) purrr * 0.3.2 2019-03-15 [1] CRAN (R 3.6.0) R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0) Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.0) readr * 1.3.1 2018-12-21 [1] CRAN (R 3.6.0) readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.0) remotes 2.0.4 2019-04-10 [1] CRAN (R 3.6.0) rlang 0.4.0.9000 2019-08-03 [1] Github (r-lib/rlang@b0905db) rmarkdown 1.16 2019-10-01 [1] CRAN (R 3.6.0) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0) rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0) rvest 0.3.4 2019-05-15 [1] CRAN (R 3.6.0) scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.0) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0) stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) testthat 2.2.1 2019-07-25 [1] CRAN (R 3.6.0) tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.0) tidyr * 1.0.0 2019-09-11 [1] CRAN (R 3.6.0) tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0) tidyverse * 1.2.1 2017-11-14 [1] CRAN (R 3.6.0) usethis 1.5.0 2019-04-07 [1] CRAN (R 3.6.0) vctrs 0.2.0.9000 2019-08-03 [1] Github (r-lib/vctrs@11c34ae) whisker 0.3-2 2013-04-28 [1] CRAN (R 3.6.0) withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) xaringan 0.9 2019-03-06 [1] CRAN (R 3.6.0) xfun 0.10 2019-10-01 [1] CRAN (R 3.6.0) xml2 1.2.0 2018-01-24 [1] CRAN (R 3.6.0) yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0) zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0) [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ``` <p></p> ::: These slides are licensed under <br><center><a href="https://creativecommons.org/licenses/by-sa/3.0/au/"><img src="images/cc.svg" style="height:2em;"/><img src="images/by.svg" style="height:2em;"/><img src="images/sa.svg" style="height:2em;"/></a></center>