r markdown - shippensburg university of...

34
R Markdown The Basics

Upload: others

Post on 14-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

R Markdown!The Basics!

Page 2: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

ThiswastheoriginalworkflowinRStudiousing

Rscriptfiles(*.R)

Page 3: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

RNotebooksbringtogetherprose,code,andresults.Theyaregreatfor•  CommunicaCngresults

tothosewhodonotneedtoseethecode

•  CollaboraCngwithotherdatascienCstsinterestedintheresultsandthecode

•  Actuallydoingdatascience

Page 4: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

R Markdown

Page 5: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Components of an R Notebook

Page 6: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

TextusingMarkdown(andLaTeX)

CodeusingR(orotherlanguages)

YAMLheader

Page 7: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Text Using Markdown

Page 8: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Headers

# Header 1

## Header 2

### Header 3

#### Header 4

##### Header 5

###### Header 6

Header 1 Header 1 Header 1 Header 1 Header 1 Header 1

Use#’stocreateheadersfor

chapters,secCons,subsecCons,etc.

Page 9: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Text

italics

bold

code

Text

Text

_italics_

_ _bold_ _

`code`

Textisrenderedasplaintext.Useunderscores(_)tomakeitalics,twounderscores(__)tomakebold,backCckstomakecode.

Addtwospacesattheendofalinetostartanewline

Page 10: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Lists

Bullet points* item 1* item 2

Numbered lists1. item 12. item 2

Bullet points•  item 1•  item 2

Numbered lists1.  item 12.  item 2

Useasteriskstomakebulletpoints.Usenumberstomakenumberedlists.

Page 11: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Hyperlinks

This is a [link](www.ship.edu).

This is a link.

Usebracketstodenotealink.PlacetheURLinparentheses.

Page 12: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

EquationsAccording to Einstein, $E=mc^2$.

According to Einstein, E = mc2.

WriteequaConswithLaTeXcommandsandsurroundthemin$’s.

Page 13: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Equation Blocks

According to Einstein, $$E=mc^2$$.

According to Einstein,

E = mc2.Usetwo$’stomakecenteredequaConblocks.

Page 14: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

TheRStudiologo.

Images

![](logo.png) The RStudio logo.

Usealinkprecededbyan!toinsertanimage.LinkshouldbeaURLorfilepath.

Page 15: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio
Page 16: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Code Chunks

Page 17: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Code Chunks

InfinalkniTeddocument(html,pdf,etc.)InRnotebook

Page 18: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Chunk Options

Forcode:evaldeterminesifthecodeisevaluated

Page 19: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Chunk Options

Forresults:echodeterminesiftheoutputisshown,messageandwarningdetermine

iftheseareshown.

Page 20: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Chunk Options

Forplots:fig.heightandfig.widthcontrolthesizeoftheplot,andfig.aligncontrols

itsposiCon

Page 21: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio
Page 22: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Inline CodeToday is`r Sys.Date()`.

Today is 2018-02-06.

Placecodeinasentencewith`r#code`.RMarkdownwillreplacethecodewithitsresults.

Page 23: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Output Format

Page 24: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Preview

WhilewriCngyourdocument,youcanperiodicallyPreviewit.

Page 25: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Knit Youcanknittohtml,pdf,orwordbyclicking

onthetriangle

Page 26: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

YAML Header

TheYAMLheaderkeepstrackoftheoutputtypes

andtheiropCons

Page 27: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

YAML Header

IftheabilitytoPreviewthenotebookdisappears,addthislinetotheYAML

Page 28: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

The setup code chunk

Page 29: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Chunk OptionsThesetupcodechunkisalwaysrunonce

beforeanyotherchunks.Inthiscourse,thiswillbethefirstcodechunkthatwillcontainthepackagesyouareusing.Youcanhidethewarningsandmessagesforloadingpackages

Page 30: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Eventually…

Page 31: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Interactive Webpages

Page 32: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Websites

Page 33: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Books

Page 34: R Markdown - Shippensburg University of Pennsylvaniawebspace.ship.edu/lebryant/mat219/pdf/slides-05-rmarkdown-basics.pdfR Markdown! The Basics! This was the original workflow in RStudio

Journal Articles