# Fundamental of Data Science for EESS <img src="img/course-logo.png" width="40%" style="display: block; margin: auto;" /> ## R session 02 - R markdown .font120[**Daniel Vaulot**] 2019-01-24 .pull-left[ <img src="img/NTU-Logo-full-colour.png" width="50%" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ <img src="img/logo_SBR.png" width="50%" style="display: block; margin: auto 0 auto auto;" /> ] --- class: middle ## Outline .font150[ * What is Markown ? * R markdown * What can you do with R markdown ? ] --- # Installation and Resources .pull-left[ ## Packages * rmarkdown (will install also knitr) * tinytex (Latex) ## Resources * On-line Book: https://bookdown.org/yihui/rmarkdown * Cheat sheet: https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf ] .pull-right[ <img src="img/R-markdown-book.png" width="65%" style="display: block; margin: auto;" /> ] --- layout: false background: url(img/logo-markdown.png) # What is markdown ? .center[ <img src="img/markdown-John_Gruber.jpg" width="20%" /><img src="img/markdown-Aaron_Swartz.jpg" width="20%" /> ] * Created in 2004 by [John Gruber](https://en.wikipedia.org/wiki/John_Gruber) and [Aaron Swartz](https://en.wikipedia.org/wiki/Aaron_Swartz) * Goal : "to write using an easy-to-read and easy-to-write plain text format, optionally convert it to structurally valid HTML". -- ## Many flavors... * MultiMarkdown * GitHub Flavored Markdown (GFM) * Pandoc * CommonMark --- # Rmarkdown <img src="img/package-rmarkdown-flow.png" width="70%" /> ## Mix * Markdown - paragraph structure - comments - links * R code ("chunks") * Output of R code --- layout: true # Your first Rmarkdown file --- .student[Who has not been able to install Rmarkdown and Latex ?] -- <img src="img/package-rmarkdown-new-01.png" width="40%" /><img src="img/package-rmarkdown-new-02.png" width="40%" /> --- ![](img/R-markdown_new.gif) --- ## Knit to HTML <img src="img/package-rmarkdown-knit.png" width="80%" /> .warning[Save to "xxx.Rmd"] --- <img src="img/package-rmarkdown-HTML.png" width="100%" /> --- ## Knit of pdf <img src="img/package-rmarkdown-knit-pdf.png" width="40%" /> --- <img src="img/package-rmarkdown-pdf-output.png" width="100%" /> --- layout: true # Markdown syntax --- * Quick guide: https://www.markdownguide.org/basic-syntax/ ## Headings ```r # Heading - level 1 ## Heading - level 2 ``` -- ## Heading - level 1 ### Heading - level 2 --- ## Paragraphs ```r Paragraphs are separated by a blank line. Two spaces at the end of a line produces a line break. ``` -- Paragraphs are separated by a blank line. Two spaces at the end of a line produces a line break. --- ## Formatting ```r _italic_, *italic*, **bold**, `monospace`. ``` -- _italic_, *italic*, **bold**, `monospace`. .warning[Do not mix straight and backward quotes] --- ## Bullet lists ```r Bullet list: * apples * oranges * pears * passe crassane (4 spaces to indent) ``` -- * apples * oranges * pears * passe crassane --- ## Numbered lists ```r Numbered list: 1. wash 1. rinse 1. repeat ``` -- 1. wash 1. rinse 1. repeat --- ## Hyperlinks ```r [Text of the link](URL of the link) # Example [Markdown syntax](https://www.markdownguide.org/basic-syntax/) ``` -- [Markdown syntax](https://www.markdownguide.org/basic-syntax/) --- ## Images ```r ![Image name](URL of the link - can also be a local file on your computer) # Example ![](https://i.pinimg.com/originals/1c/b7/24/1cb724b545de717ed0430e9d46e78eea.jpg) ``` -- ![](https://i.pinimg.com/originals/1c/b7/24/1cb724b545de717ed0430e9d46e78eea.jpg) --- ## Tables ```r ID | First | Last --- | --- | --- 1 | Donald | Trump ``` -- ID | First | Last --- | --- | --- 1 | Donald | Trump --- layout: false # Rmarkdown conversion <img src="img/R-markdown-flow.png" width="70%" style="display: block; margin: auto;" /> -- .pull-left[ * [knitr](https://yihui.name/knitr/) : R library * [Pandoc](https://pandoc.org/) : command line tool * Converts from **md** to **pdf**, **html**, **docx**... ```r # HTML > pandoc test1.md -f markdown -t html -s -o test1.html # pdf > pandoc test1.md -s -o test1.pdf ``` ] .pull-right[ <img src="img/package-knitr-logo.png" width="30%" style="display: block; margin: auto;" /> <img src="img/pandoc.png" width="100%" style="display: block; margin: auto auto auto 0;" /> ] --- layout: true # The Rmarkdown file --- ## File structure <img src="img/package-rmarkdown-structure.png" width="100%" /> --- ## Knit process <img src="img/package-rmarkdown-md-window.png" width="100%" /> --- ## Output <img src="img/package-rmarkdown-output.png" width="100%" /> --- layout: true # Chunks --- ## Insert chunk <img src="img/package-rmarkdown-chunk-01.png" width="100%" /> --- ## Options <img src="img/package-rmarkdown-chunk-options.png" width="100%" /> --- ## Run chunk * Two options <img src="img/package-rmarkdown-chunk-write.png" width="100%" /> --- ## Run chunk inside Rmd file <img src="img/package-rmarkdown-chunk-run.png" width="100%" /> --- ## Knit chunk to HTML <img src="img/package-rmarkdown-chunk-run-knit.png" width="50%" /> --- ## Useful options <br> Options | Default value | Aim --- | --- | --- echo | TRUE | Print code (= FALSE in a report for example) eval | TRUE | Evaluate code (= FALSE if want to show code only) warning | TRUE | Warning message (= FALSE to remove long warnings) message | TRUE | Messages (= FALSE to remove long messages) cache | FALSE | If TRUE only modified chunks will be evaluated | | very useful for computing heavy codes fig.height | | inches fig.width | | inches --- ## Set defaults options * Insert in a R chunk at the beginning of your Rmarkdownn file ```r knitr::opts_chunk$set(fig.width = 6, fig.height = 6, eval = TRUE, cache = TRUE, echo = TRUE, ) ``` --- layout: true # What can you do with Rmarkdown ? --- ## Presentation .left-column[ <img src="img/package-xaringan-logo.png" width="45%" /> ] .right-column[ https://github.com/yihui/xaringan <img src="img/package-xaringan-example.png" width="80%" /> ] --- ## Posters .left-column[ <img src="img/package-posterdown-logo.png" width="45%" /> ] .right-column[ https://github.com/brentthorne/posterdown <img src="img/package-posterdown-example.png" width="80%" /> ] --- ## Curriculum vitae .left-column[ <img src="img/package-vitae-logo.png" width="45%" /> ] .right-column[ https://cloud.r-project.org/web/packages/vitae/index.html <img src="img/package-vitae-example-02.png" width="90%" /> ] --- ## Website .left-column[ <img src="img/package-blogdown-logo.png" width="45%" /> ] .right-column[ https://bookdown.org/yihui/blogdown/ <img src="img/web-site-dv.png" width="90%" style="display: block; margin: auto;" /> ] --- layout: false # Next time: Data manipulation (tidyr) .pull-left[ What you will learn : * Read data from files (text, Excel) * Manipulate data - create new columns - filter - summarize (statistics) ] .pull-right[ <img src="img/R_for_datascience.png" width="40%" style="display: block; margin: auto;" /> ] .student[ * Please install the following packages and their dependencies * readr * readxl * dplyr * tidyr * stringr * Download data files (links on slack) * Chapter 5 of R for data science: https://r4ds.had.co.nz/ ]