biost 561: r markdown intro - university of...
TRANSCRIPT
![Page 1: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/1.jpg)
BIOST 561: R Markdown Intro
David Whitney
November 3, 2016
![Page 2: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/2.jpg)
R Markdown: The Basics
The following information is readily available if you use Rstudio:
Markdown is a simple formatting syntax for authoring HTML,PDF, and MS Word documents.For more details on using R Markdown seehttp://rmarkdown.rstudio.com.When you click the Knit button a document will be generatedthat includes both content as well as the output of anyembedded R code chunks within the document.
![Page 3: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/3.jpg)
Supported Output Formats
R Markdown can output to 3 standard formats.
HTML document, with presentations via ioslides or SlidyPDF document, with presentations via beamerMS Word document
Note: documents/presentations with Shiny apps possible (notcovered herein).
![Page 4: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/4.jpg)
Basic Markdown Syntax
Regardless of your chosen output format, some basic syntax will beuseful:
Section headersText emphasisListsR code
![Page 5: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/5.jpg)
Section Headers
To set up different sized header text in your document, use # forHeader 1, ## for Header 2, and ### for Header 3.
In a presentation, this creates a new slide.
![Page 6: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/6.jpg)
Text emphasis
Italicize text via *Italicize* or _Italicize_.Bold text via **Bold** or __Bold__.
![Page 7: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/7.jpg)
Unordered Lists
This code
* Item 1* Item 2
+ Item 2a+ Item 2b
Renders these bullets (note that the sub-list needs tabulation, notspaces!)
Item 1Item 2
Item 2aItem 2b
![Page 8: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/8.jpg)
Ordered Lists
This code
1. Item 12. Item 2
+ Item 2a+ Item 2b
Renders this list (be advised - the bullets may not look great in alltemplates)
1 Item 12 Item 2
Item 2aItem 2b
![Page 9: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/9.jpg)
Inline R Code
To use R within a line, use the syntax ‘r foo‘.e.g. ‘r round(pi, 5)‘ renders as 3.14159.
![Page 10: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/10.jpg)
R Code Chunks
R code chunks let you run/render code and results similar toSweave or knitr.To start a code chunk, use the syntax “‘{r chunkName,options}.To end the chunk, type “‘.
![Page 11: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/11.jpg)
Example Chunk Output 1
With no options specified, a typical code chunk might look like:
```{r pressure}summary(pressure)```
## temperature pressure## Min. : 0 Min. : 0.0002## 1st Qu.: 90 1st Qu.: 0.1800## Median :180 Median : 8.8000## Mean :180 Mean :124.3367## 3rd Qu.:270 3rd Qu.:126.5000## Max. :360 Max. :806.0000
![Page 12: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/12.jpg)
Example Chunk Output 2
Want to display the output of a code chunk and the underlyingR code?Specify the echo = TRUE option.
summary(pressure)
## temperature pressure## Min. : 0 Min. : 0.0002## 1st Qu.: 90 1st Qu.: 0.1800## Median :180 Median : 8.8000## Mean :180 Mean :124.3367## 3rd Qu.:270 3rd Qu.:126.5000## Max. :360 Max. :806.0000
![Page 13: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/13.jpg)
Example Chunk Output 3
R code chunks can also be used to render plots.
plot(pressure)
0 50 100 150 200 250 300 350
020
040
060
080
0
temperature
pres
sure
![Page 14: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/14.jpg)
Example Chunk Output 4
Want to display R code without evaluating it?Specify eval = FALSE, echo = TRUE in the chunk options:
summary(pressure)
![Page 15: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/15.jpg)
Mathematical Symbols/Equations in Markdown
LATEX’s inline (e.g. $foo$) and display (e.g. $$foo$$) mathmodes are supported in Markdown for output to HTML, Wordor PDF.
![Page 16: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/16.jpg)
Tables and Figures in Markdown
The default R output suffices for teaching, but requires cleaningup for assignments, theses, or papers.This is when knitting to *.pdf becomes an attractive option.
![Page 17: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/17.jpg)
Knitting
![Page 18: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/18.jpg)
LATEX and Markdown
Rendering Markdown as a pdf requires a LATEX installation (seeKatie’s slides from last week!).You will additionally need to install Pandoc fromhttp://pandoc.org/With LATEX, many customizations are possible.
![Page 19: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/19.jpg)
LATEX Customization, 1
You can include additional LATEX directives and/or content, orreplace the core pandoc template entirely.Use the includes option as follows to add your favorite filesfor the preamble, title/abstract, bibliography, etc. . .
---title: 'A More Organized Person's Document'output:
beamer_presentation:includes:
in_header: header.texbefore_body: doc_prefix.texafter_body: doc_suffix.tex
---
![Page 20: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/20.jpg)
LATEX Customization, 2
If you are instead a creature of (bad) habit, you may opt forthe header-includes option over the modular approach:
---title: 'BIOST 561: R Markdown Intro'author: "David Whitney"date: "November 3, 2016"header-includes:
- \usepackage{graphicx}output:
beamer_presentation:theme: "Frankfurt"
---
![Page 21: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/21.jpg)
Note: LATEXin Text
In Markdown, “\LaTeX rocks” renders as “LATEXrocks” (nospace!).Use “\LaTeX\ rocks” to render “LATEX rocks”, instead.This can be especially important when using new commands.
![Page 22: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/22.jpg)
Tables
Customization for tables can be carried out using functions in theknitr or xtable packages from R.
```{r table}knitr::kable(summary(pressure))```
```{r table, results='asis'}xtable::xtable(summary(pressure))```
![Page 23: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/23.jpg)
Example Output: knitr
temperature pressure
Min. : 0 Min. : 0.00021st Qu.: 90 1st Qu.: 0.1800Median :180 Median : 8.8000Mean :180 Mean :124.33673rd Qu.:270 3rd Qu.:126.5000Max. :360 Max. :806.0000
![Page 24: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/24.jpg)
Example Output: xtable
% latex table generated in R 3.2.3 by xtable 1.8-2 package % ThuNov 03 00:18:54 2016
temperature pressure1 Min. : 0 Min. : 0.00022 1st Qu.: 90 1st Qu.: 0.18003 Median :180 Median : 8.80004 Mean :180 Mean :124.33675 3rd Qu.:270 3rd Qu.:126.50006 Max. :360 Max. :806.0000
![Page 25: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/25.jpg)
Customizing Figures: Captions
The fig.cap option allows you to specify the caption for the figuregenerated by a given chunk:
```{r caption, fig.cap="I am the caption."}plot(pressure)```
![Page 26: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/26.jpg)
Caption Example
0 50 100 150 200 250 300 350
020
040
060
080
0
temperature
pres
sure
Figure 1: I am the caption.
![Page 27: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/27.jpg)
Customizing Figures: Size
The fig.height and fig.width options let you specify thedimensions of your plots:
```{r caption, fig.height = 4, fig.width = 8}plot(pressure)```
![Page 28: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/28.jpg)
Figure Size Example
0 50 100 150 200 250 300 350
020
040
060
080
0
temperature
pres
sure
![Page 29: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/29.jpg)
Additional Resources
RStudio: http://rmarkdown.rstudio.com/xtable and knitr documentationPandoc: http://pandoc.org/Google
![Page 30: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/30.jpg)
Next Week’s Topic
Unix system, shell scripts, cluster computing
![Page 31: BIOST 561: R Markdown Intro - University of Washingtonfaculty.washington.edu/kenrice/sisg-adv/markdownNotes.pdf · 2017. 7. 13. · Supported Output Formats RMarkdowncanoutputto3standardformats](https://reader034.vdocuments.mx/reader034/viewer/2022052100/603961bfa64f481b3221dcfb/html5/thumbnails/31.jpg)
Any Questions?