layout: true background-image: url(img/course-logo.png), url(img/logo_SBR.png), url(img//NTU-Logo-full-colour.png) background-position: right top 30px, right 50px bottom 50px,left 50px bottom 50px, top 350px left 500px background-size: 45%, 25%, 20% # Fundamentals of Data Science for EESS --- <br> <br> <br> <br> ## R session 02 - R markdown .font120[**Daniel Vaulot**] 2021-01-20 --- layout: false class: inverse # Outline <br> <br> .font150[ * What is Markown ? * Rmarkdown syntax * R chunks * Some applications ] --- layout: true # 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 | Jo | Biden ``` -- ID | First | Last --- | --- | --- 1 | Jo | Biden --- 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="80%" /> --- ## Knit process <img src="img/package-rmarkdown-md-window.png" width="80%" /> --- ## Output <img src="img/package-rmarkdown-output.png" width="80%" /> --- layout: true # R Chunks --- ## Insert R chunk <img src="img/package-rmarkdown-chunk-01.png" width="80%" /> --- ## Run R chunk * Two options <img src="img/package-rmarkdown-chunk-write.png" width="70%" /> --- ## Option 1: Run R chunk inside Rmd file - Use when building and debugging an Rmd file <img src="img/package-rmarkdown-chunk-run.png" width="80%" /> --- ## Option 2: Knit R chunk to HTML - Use for final production <img src="img/package-rmarkdown-chunk-run-knit.png" width="50%" /> --- ## Options for R chunks <img src="img/package-rmarkdown-chunk-options.png" width="80%" /> --- exclude: false ## 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 --- exclude: false ## Set defaults options * Insert in a R chunk at the beginning of your Rmarkdown file * Option CACHE = true reduce compiling time ```r knitr::opts_chunk$set(fig.width=6, fig.height=6, eval=TRUE, cache=TRUE, echo=TRUE,) ``` --- exclude: false layout: true # What can you do with Rmarkdown ? --- background-image: url(img/R-markdown-document-01.png), url(img/R-markdown-document-02.png) background-position: right 20px bottom 20px, left 20px bottom 20px background-size: 40%, 50% ## Document your data analyses * If the data changes, you can re-run analysis in a matter of minutes * More and more journal request analyses scripts --- exclude: false background-image: url(img/package-xaringan-example.png) background-position: right 20px top 20px background-size: 60% ## Presentation .left-column[ <img src="img/package-xaringan-logo.png" width="60%" /> https://github.com/yihui/xaringan ] --- exclude: false background-image: url(img//package-posterdown-example.png) background-position: right 20px top 20px background-size: 60% ## Posters .left-column[ <img src="img/package-posterdown-logo.png" width="60%" /> https://github.com/brentthorne/posterdown ] --- exclude: false background-image: url(img/package-vitae-example-02.png) background-position: right 20px top 20px background-size: 50% ## Curriculum vitae .left-column[ <img src="img/package-vitae-logo.png" width="60%" /> https://cloud.r-project.org/web/packages/vitae/index.html ] --- background-image: url(img//web-site-pr2.png) background-position: bottom 20px right 20px background-size: 45% exclude: false ## Website .left-column[ <img src="img/package-blogdown-logo.png" width="60%" /> https://bookdown.org/yihui/blogdown/ ] --- layout: false class: inverse # What did you learn ? <br> <br> <br> .font150[ - Rmarkdown: mix text, R chunk, R output ] -- .font150[ - Compile to HTML or to PDF ] -- .font150[ - Can be used for many different purposes ] -- .font150[ - Use to document your analysis process (for papers...) ]