m01 lesson 01

27
Module 01 Lesson 1: R Markdown Nik Bear Brown  July 28, 2015 Lesson 1: R Markdown In the frst lesson, we start the module discussing R arkdown to create written re!orts with em"edded working code# R arkdown is an authoring $ormat that ena"les easy creation o$ dynamic documents, !resentations, and re!orts $rom R# It com"ines the core synta% o$ markdown &an easy'to'write !lain te%t $ormat( with em"edded R code chunks that are run so their out!ut can "e included in the fnal document# R arkdown documents can "e regenerated whene)er underlying R code or data changes#  *his is an R arkdown document# arkdown is a sim!le $or matting synta% $or authoring +*, -./, and or d documents# /or more details on using R arkdown see htt!33rmarkdown#rstudio#com # In odule 01 esson 1 we will learn a"out R arkdown synta%# *he $ollowing lessons and assingments all will use R arkdown# R arkdown is an authoring $ormat that ena"les easy creation o$ dynamic documents, !resentations, and re!orts $rom R# It com"ines the core synta% o$ markdown &an easy'to'write !lain te%t $ormat( with em"edded R code chunks that are run so their out!ut can "e included in the fnal document# R arkdown documents are $ully re!roduci"le &they can "e automatically regenerated whene)er underlying R code or data changes(#  *he Rarkdown we"site & htt!33rmarkdown#rstudio#com3 ( descri"es R arkdown )2, a ne%t generation im!lementation o$ R arkdown "ased on knitr and !andoc# *his im!lementation "rings many enhancements to R arkdown, including any a)aila"le out!ut $ormats including +*, -./, and ord# u!!ort $or creating Beamer, ioslides, and lidy !resentations# New markdown synta% including e%!anded su!!ort $or ta"les and "i"liogra!hies# 4 +ooks $ or cust omi in g +* a nd - ./ o ut!ut & inc lude 6, headers, and $ooters(# 4 Include raw a* e7 withi n mar kdown $or ad)anced customi ation o $ -./ out!ut#

Upload: romil-shah

Post on 28-Feb-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 1/27

Module 01 Lesson 1: R Markdown

Nik Bear Brown

 July 28, 2015

Lesson 1: R Markdown

In the frst lesson, we start the module discussing R arkdown tocreate written re!orts with em"edded working code# R arkdown is anauthoring $ormat that ena"les easy creation o$ dynamic documents,!resentations, and re!orts $rom R# It com"ines the core synta% o$markdown &an easy'to'write !lain te%t $ormat( with em"edded R codechunks that are run so their out!ut can "e included in the fnaldocument# R arkdown documents can "e regenerated whene)erunderlying R code or data changes#

 *his is an R arkdown document# arkdown is a sim!le $ormattingsynta% $or authoring +*, -./, and ord documents# /or moredetails on using R arkdown see htt!33rmarkdown#rstudio#com#

In odule 01 esson 1 we will learn a"out R arkdown synta%# *he$ollowing lessons and assingments all will use R arkdown#

R arkdown is an authoring $ormat that ena"les easy creation o$dynamic documents, !resentations, and re!orts $rom R# It com"inesthe core synta% o$ markdown &an easy'to'write !lain te%t $ormat( withem"edded R code chunks that are run so their out!ut can "e includedin the fnal document# R arkdown documents are $ully re!roduci"le&they can "e automatically regenerated whene)er underlying R code ordata changes(#

 *he Rarkdown we"site &htt!33rmarkdown#rstudio#com3( descri"es Rarkdown )2, a ne%t generation im!lementation o$ R arkdown "asedon knitr and !andoc# *his im!lementation "rings many enhancementsto R arkdown, including

any a)aila"le out!ut $ormats including +*, -./, and ord#

u!!ort $or creating Beamer, ioslides, and lidy !resentations# Newmarkdown synta% including e%!anded su!!ort $or ta"les and"i"liogra!hies#

4 +ooks $or customiing +* and -./ out!ut &include 6, headers,and $ooters(#

4 Include raw a*e7 within markdown $or ad)anced customiation o$-./ out!ut#

Page 2: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 2/27

4 6om!ile +*, -./, or ord note"ooks $rom R scri!ts#%tensi"lity create custom tem!lates and e)en entirely newout!ut $ormats#

4 6reate interacti)e R arkdown documents using hiny#

Note that -./ out!ut &including Beamer slides( re9uires a $ullinstallation o$ *e7#

ee :Rarkdown; &htt!33rmarkdown#rstudio#com3(

Installing R, RStudio, & RMarkdown4 Install R

< R $or ac &htt!s33cran#r'!ro=ect#org3"in3macos%3(

< R $or indows &htt!s33cran#r'!ro=ect#org3"in3windows3"ase3(

< R $or inu% &htt!s33cran#r'!ro=ect#org3"in3linu%3(

4 Install the lastest )ersion o$ Rtudio4 Install the latest )ersion o$ the rmarkdown !ackage

install#!ackages&>rmarkdown>(

Opening R Markdown document

?!en the #Rmd document in R studio and 6lick on >@nit +*> or crtl'shi$t'@ 

R Markdown Cheat Sheets

R arkdown 6heat heet &-./(, a 9uick guide to the most commonlyused markdown synta%, knitr o!tions, and out!ut $ormats# R arkdown6heat heet &-./(

Page 3: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 3/27

R arkdown Re$erence Auide &-./(, a more com!rehensi)e re$erenceguide to markdown, knitr, and out!ut $ormat o!tions# R arkdownRe$erence Auide &-./(

Page 4: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 4/27

dditional packages needed

 *o run the code in 01esson01#Rmd you may need additional!ackages#

4 I$ necessary install gg!lot2 and lattice !ackages

install#!ackages&>gg!lot2>(C install#!ackages&>lattice>(

Creating a new R Markdown document

 *o create a new R arkdown document

4 ?!en R tudio, and go to /ile ' New ' R arkdown

4 -aste in the contents o$ this gist &which contains the R arkdownfle used to !roduce this !ost( and sa)e the fle with an #rmd e%tension

4 6lick @nit +*

dditional R Markdown document options

6lick on gear to the right o$ >@nit +*> to see the o!tions#

Page 5: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 5/27

?!tions such as ta"le o$ contents, using a 6 style sheet, etc#

Con!ert Markdown "les to #ord, $%

 *o switch "etween the out!ut o$ +*, -./, and ord# =lick on gearto the right o$ >@nit +*> to see the o!tions#

Page 6: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 6/27

ord

-./

Looading R li'raries

set(seed&DDD(li'rar)&gg!lot2(li'rar)&lattice(

Looading datadata&diamonds(

Page 7: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 7/27

R Code Chunks *em'edding R+

ithin an R arkdown fle, R 6ode 6hunks can "e em"edded using thenati)e arkdown synta% $or $enced code regions# Eou can em"ed an Rcode chunk like this

summar)&cars(

FF s!eed distFF in# G#0 in# 2#00FF 1st Hu#12#0 1st Hu# 2#00FF edian 15#0 edian D#00FF ean 15#G ean G2#8FF Drd Hu#1#0 Drd Hu# 5#00FF a%# 25#0 a%# 120#00

summar)&diamonds(

FF carat cut color clarityFF in# 0#2000 /air 110 . KK5 I1 1D05FF 1st Hu#0#G000 Aood G0 KK L2 12258FF edian 0#K000 Lery Aood12082 / 5G2 I2 1GFF ean 0#KK -remium 1DK1 A1122 L1 81K1FF Drd Hu#1#0G00 Ideal 21551 + 8D0G LL2 50FF a%# 5#0100 I 5G22 LL1 D55FF J 2808 &?ther( 25D1FF de!th ta"le !rice %FF in# GD#00 in# GD#00 in# D2 in# 0#000FF 1st Hu#1#00 1st Hu#5#00 1st Hu# 50 1st Hu# G#K10FF edian 1#80 edian 5K#00 edian 2G01 edian 5#K00

FF ean 1#K5 ean 5K#G ean DDD ean 5#KD1FF Drd Hu#2#50 Drd Hu#5#00 Drd Hu# 5D2G Drd Hu# #5G0FF a%# K#00 a%# 5#00 a%# 1882D a%# 10#KG0FFFF y FF in# 0#000 in# 0#000FF 1st Hu# G#K20 1st Hu# 2#10FF edian 5#K10 edian D#5D0FF ean 5#KD5 ean D#5DFF Drd Hu# #5G0 Drd Hu# G#0G0FF a%# 58#00 a%# D1#800FF

str&diamonds(

FF Mdata#$rameM 5DG0 o"s# o$ 10 )aria"lesFF carat num 0#2D 0#21 0#2D 0#2 0#D1 0#2G 0#2G 0#2 0#22 0#2D ###FF cut ?rd#$actor w3 5 le)els >/air>O>Aood>O## 5 G 2 G 2 D D D 1 D ###FF color ?rd#$actor w3 K le)els >.>O>>O>/>O>A>O## 2 2 2 K K 5 25 ###FF clarity ?rd#$actor w3 8 le)els >I1>O>I2>O>I1>O## 2 D 5 G 2 K D G 5 ###

Page 8: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 8/27

FF de!th num 1#5 5#8 5# 2#G D#D 2#8 2#D 1# 5#1 5#G ###FF ta"le num 55 1 5 58 58 5K 5K 55 1 1 ###FF !rice int D2 D2 D2K DDG DD5 DD DD DDK DDK DD8 ###FF % num D#5 D#8 G#05 G#2 G#DG D#G D#5 G#0K D#8K G ###FF y num D#8 D#8G G#0K G#2D G#D5 D# D#8 G#11 D#K8 G#05 ###FF num 2#GD 2#D1 2#D1 2#D 2#K5 2#G8 2#GK 2#5D 2#G 2#D ###

names&diamonds(

FF :1; >carat> >cut> >color> >clarity> >de!th> >ta"le> >!rice>FF :8; >%> >y> >>

head&diamonds(

FF carat cut color clarity de!th ta"le !rice % y FF 1 0#2D Ideal I2 1#5 55 D2 D#5 D#8 2#GDFF 2 0#21 -remium I1 5#8 1 D2 D#8 D#8G 2#D1FF D 0#2D Aood L1 5# 5 D2K G#05 G#0K 2#D1FF G 0#2 -remium I L2 2#G 58 DDG G#20 G#2D 2#D

FF 5 0#D1 Aood J I2 D#D 58 DD5 G#DG G#D5 2#K5FF 0#2G Lery Aood J LL2 2#8 5K DD D#G D# 2#G8

 Eou can !lot

plot&carat, !rice, dataPdiamonds, colorPcut(

Page 9: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 9/27

 Eou can also em"ed !lots, $or e%am!le &Qr, echoP/S(

Page 10: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 10/27

Note that the echo P / !arameter was added to the code chunk to!re)ent !rinting o$ the R code that generated the !lot#

/or e%am!le, the $ollowing code

Inline R Code

 Eou can also e)aluate R e%!ressions inline "y enclosing the e%!ressionwithin a single "ack'tick 9ualifed with MrM# /or e%am!le, the $ollowingcode

arkdown Inline

Results in this out!ut >I counted 2 red trucks on the highway#>

-nitting

hen you click the -nit "utton a document will "e generated thatincludes "oth content as well as the out!ut o$ any em"edded R codechunks within the document#

Page 11: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 11/27

.nderstand the 'asics o/ Lae

Tnderstand the "asics o$ a*e7

2asic console output

 *o insert an R code chunk, you can ty!e it manually or =ust !ress6hunks ' Insert chunks or use the shortcut key# *his will !roduce the$ollowing code chunk

-ressing ta" when inside the "races will "ring u! code chunk o!tions#

 *he $ollowing R code chunk la"elled "asicconsole is as $ollows

UUUr% O' 110y O' round&rnorm&10, %, 1(, 2(d$ O' data#$rame&%, y(

d$ UUU

UUUFF % yFF 1 1 0#2FF 2 2 D#DFF D D 0#5FF G G G#28FF 5 5 D#GKFF 5#KDFF K K 8#2D

FF 8 8 8#DFF #D5FF 10 10 #GGUUU

 *he code chunk in!ut and out!ut is then dis!layed as $ollows

  % O' 110  y O' round&rnorm&10, %, 1(, 2(  d$ O' data(/rame&%, y(  d$ 

FF % yFF 1 1 '0#12FF 2 2 1#1DFF D D D#0GFF G G D#G2FF 5 5 G#18FF #11FF K K K#DFF 8 8 8#05

Page 12: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 12/27

FF 10#DKFF 10 10 #2

S)nta3

-lain te%t

nd a line with two s!aces to start a new !aragra!h#

m!hasis, aka italics, with VasterisksV or underscores#

W Block9uote

VitalicsV and italics 

VV"oldVV and "old 

trong em!hasis, aka "old, with VVasterisksVV or underscores#

6om"ined em!hasis with VVasterisks and underscoresVV#

su!erscri!tX2X

trikethrough uses two tildes# YYcratch this#YY

YYstrikethroughYY

m!hasis, aka italics, with asterisks or underscores#

Block9uote

italics and italics

'old and 'old

trong em!hasis, aka "old, with asterisks or underscores#

6om"ined em!hasis with asterisks and underscores#

su!erscri!t2

trikethrough uses two tildes# cratch this#

strikethroughInline UcodeU has U"ack'ticks aroundU it#

:link;&www#rstudio#com(

F +eader 1

FF +eader 2

Page 13: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 13/27

FFF +eader D

FFFF +eader G

FFFFF +eader 5

FFFFFF +eader

endash ''emdash '''elli!sis ###

inline e9uation P Z!iVrXQ2S

horiontal rule &or slide "reak(

VVV

W "lock 9uote

V unordered list

V item 2  [ su"'item 1  [ su"'item 2

1# ordered list2# item 2  [ su"'item 1  [ su"'item 2

im!le dot !oints V -oint 1V -oint 2V -oint D and numeric dot !oints 

1# Num"er 12# Num"er 2D# Num"er D and nested dot !oints V   V #1  V #2

Page 14: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 14/27

V B  V B#1  V B#2 

 *a"le +eader \ econd +eader

''''''''''''' \ ''''''''''''' *a"le 6ell \ 6ell 26ell D \ 6ell G

Inline code has "ack'ticks around it#

link

4eader 1

4eader 5

4eader 6

4eader 7

Header 5

+eader

endash '' emdash ''' elli!sis ###

inline e9uation  A=π ∗r2

horiontal rule &or slide "reak(

"lock 9uote

4 unordered list

4 item 2

< su"'item 1

< su"'item 21 ordered list

2 item 2

< su"'item 1

< su"'item 2

im!le dot !oints

Page 15: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 15/27

4 -oint 1

4 -oint 2

4 -oint D

and numeric dot !oints

1 Num"er 12 Num"er 2

D Num"er D

and nested dot !oints

4

< #1

< #2

4 B

< B#1

< B#2

 *a"le +eader

 *a"le 6ell

6ell D

$lots

Images generated "y knitr are sa)ed in a fgures $older# +owe)er, theyalso a!!ear to "e re!resented in the +* out!ut using a data TRIscheme# *his means that you can !aste the +* into a "log !ost or

discussion $orum and you donMt ha)e to worry a"out fnding a !lace tostore the imagesC theyMre em"edded in the +*#

Simple plot

+ere is a "asic !lot using "ase gra!hics

plot&!rice, dataPdiamonds, colourPclarity, geomP>dot!lot>(

FF stat"indot "inwidth de$aulted to range3D0# Tse M"inwidth P %M to ad=ustthis#

Page 16: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 16/27

plot&carat, !rice, dataPdiamonds, colourPclarity([ scale8colour8'rewer&!aletteP>et1>(

Page 17: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 17/27

Note that unlike traditional wea)e, there is no need to write fgP*RT#

Control "gure si9e *he $ollowing is an e%am!le o$ a smaller fgure using fg#width andfg#height o!tions#

, fg#widthPD, fg#heightPD 9!lot&carat, !rice, dataPdiamonds, colourPclarity([ scalecolour"rewer&!aletteP>et1>(

plot&carat, !rice, dataPdiamonds, colourPclarity([ scale8colour8'rewer&!aletteP>et1>(

Page 18: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 18/27

R Code chunk /eatures

Create Markdown code /rom R

 *he $ollowing code hides the command in!ut &i#e#, echoP/(, andout!uts the content directly as code &i#e#, resultsPasis, which is similar

to resultsPte% in wea)e(#

+ere are some dot !oints

V *he )alue o$ y:1; is '0#12V *he )alue o$ y:2; is 1#1DV *he )alue o$ y:D; is D#0G

+ere are some dot !oints

4 *he )alue o$ y:1; is '0#12

4 *he )alue o$ y:2; is 1#1D

4 *he )alue o$ y:D; is D#0G

Create Markdown ta'le code /rom R

% \ y''' \ '''1 \ '0#122 \ 1#1DD \ D#0G

Page 19: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 19/27

G \ D#G25 \ G#18 \ #11K \ K#D8 \ 8#05 \ 10#DK

10 \ #2

% \ y ''' \ ''' 1 \ '0#12 2 \ 1#1D D \ D#0G G \ D#G2 5 \ G#18 \ #11 K \ K#D8 \ 8#05 \ 10#DK 10 \ #2

Control output displa)

 *he $olllowing code su!resses dis!lay o$ R in!ut commands &i#e#,echoP/( and remo)es any !receding te%t $rom console out!ut&commentP>>C the de$ault is commentP>FF>(#

UUU

  % y1 1 '0#122 2 1#1DD D D#0GG G D#G25 5 G#18 #11UUU

  % y1 1 '0#122 2 1#1D

D D D#0GG G D#G25 5 G#18 #11

Cache anal)sis

6aching analyses is straight$orward# +ereMs e%am!le code# ?n the frstrun on my com!uter, this took a"out 10 seconds# ?n su"se9uent runs,this code was not run#

I$ you want to rerun cached code chunks, =ust delete the contents o$

the cache $olderUUUr$or &i in 15000( Q  lm&&i[1(Yi(SUUU

Page 20: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 20/27

uations

ulti!le synta%es e%ist

dollars inline  and

displa y

# &same as a*e7( single "ackslash inline  and

display

&same as a*e7( dou"le "ackslash Z&inlineZ( and Z:dis!layZ; naked late%en)ironment

#

9uations are included "y using a*e7 notation and including them

either "etween single dollar signs &inline e9uations( or dou"le dollarsigns &dis!layed e9uations(# I$ you hang around the H] site6rossLalidated youMll "e $amiliar with this idea#

 *here are inline e9uations such as  y i=α + β x i+e i #

nd dis!layed $ormulas

1

1+exp (− x)

)pesetting uations

Inline !s( %ispla) Material

9uations can "e $ormatted inline or as displayed formulas# In thelatter case, they are centered and set o^ $rom the main te%t# In the$ormer case, the mathematical material occurs smoothly in the line o$te%t#

In order to ft neatly in a line, summation e%!ressions &and similar

constructs( are $ormatted slightly di^erently in their inline and dis!lay)ersions#

Inline mathematical material is set o^ "y the use o$ single dollar'signcharacters# 6onse9uently, i$ you wish to use a dollar sign &$or e%am!le,to indicate currency(, you need to !re$ace it with a "ack'slash# *he$ollowing e%am!les, $ollowed "y their ty!eset )ersions, should makethis clear

Page 21: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 21/27

 *his summation e%!ression ZsumQiP1SXn 7i a!!ears inline#

 *his summation e%!ression ∑i=1

n

 X i  a!!ears inline#

 *his summation e%!ression is in dis!lay $orm#

ZsumQiP1SXn 7i

 *his summation e%!ression is in dis!lay $orm#

∑i=1

n

 X i

Some Lae 2asics

In this section, we show you some rudiments o$ the a*e7 ty!esettinglanguage#

Su'scripts and Superscripts

 *o indicate a su"scri!t, use the underscore   character# *o indicate asu!erscri!t, use a single caret character X# Note this can "e con$using,"ecause the R arkdown language delimits su!erscri!ts with twocarets# In a*e7 e9uations, a single caret indicates the su!erscri!t#

I$ the su"scri!t or su!erscri!t has =ust one character, there is no needto delimit with "races# +owe)er, i$ there is more than one character,

"races must "e used#

 *he $ollowing e%am!les illustrate

7i7QiS

 X i

 X i

Notice that in the a"o)e case, "races were not actually needed#In this ne%t e%am!le, howe)er, $ailure to use "races creates an error,as a*e7 sets only the frst character as a su"scri!t

7Qi,=S7i,=

Page 22: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 22/27

 X i , j

 X i , j

+ere is an e%!ression that uses "oth su"scri!ts and su!erscri!ts7X2Qi,=S

 X i , j2

Suare Roots

e indicate a s9uare root using the Zs9rt o!erator#

Zs9rtQ"X2 ' GacS

√ b2

−4

ac

ractions

.is!layed $ractions are ty!eset using the Z$rac o!erator#

Z$racQGXDSQ1S

4 z3

16

Summation 3pressions *hese are indicated with the UM o!erator, $ollowed "y a su"scri!t $or thematerial a!!earing "elow the summation sign, and a su!erscri!t $orany material a!!earing a"o)e the summation sign#

+ere is an e%am!le#

ZsumQiP1SXQnS 7XDi

∑i=1

n

 X i3

Sel/;Si9ing $arentheses

In a*e7, you can create !arentheses, "rackets, and "races which siethemsel)es automatically to contain large e%!ressions# Eou do thisusing the Zle$t and Zright o!erators# +ere is an e%am!le

ZsumQiP1SXQnSZle$t& Z$racQ7iSQEiS Zright(

Page 23: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 23/27

∑i=1

n

( X iY i)

<reek Letters

any statistical e%!ressions use Areek letters# uch o$ the Areekal!ha"et is im!lemented in a*e7, as indicated in the a*e7 cheatsheet a)aila"le at the course we"site# *here are "oth u!!er and lowercase )ersions a)aila"le $or some letters#

Zal!ha, Z"eta, Zgamma, ZAamma

α , β , γ , Γ  

Special S)m'ols

ll common mathematical sym"ols are im!lemented, and you can fnda listing on a a*e7 cheat sheet#

V u"scri!ts to get Z& aQ"S Z( write aQ"SV u!erscri!ts write Z& aXQ"S Z( write aXQ"SV Areek letters like Z& Zal!ha, Z"eta, Zldots Z( write Zal!ha, Z"eta, ZldotsV ums like Z& ZsumQnP1SXN Z( write ZsumQnP1SXNV ulti!lication like Z& Ztimes Z( write ZtimesV -roducts like Z& Z!rodQnP1SXN Z( write Z!rodQnP1SXNV Ine9ualities like Z& O, Zle9, Zge9 Z( write O, Zle9, Zge9V .istri"uted like Z& Zsim Z( write ZsimV +ats like Z& ZwidehatQZal!haS Z( write ZwidehatQZal!haS

V )erages like Z& Z"arQ%S Z( write Z"arQ%SV /ractions like Z& Z$racQaSQ"S Z( write Z$racQaSQ"S

4 u"scri!ts to get & aQ"S ( write ab

4 u!erscri!ts write & aXQ"S ( write ab

4 Areek letters like & , , ( write α , β , …

4 ums like & QnP1SXN ( write ∑n=1

 N 

4 ulti!lication like & ( write ×

4 -roducts like & QnP1SXN ( write ∏n=1

 N 

4 Ine9ualities like & O, , ( write ¿ , ≤ , ≥

Page 24: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 24/27

4 .istri"uted like & ( write ∼

4 +ats like & ( write α 

4 )erages like & Q%S ( write  x

4 /ractions like & ( writeab

ome e%am!les# &Notice that, in the third e%am!le, I use the tildecharacter $or a $orced s!ace# Aenerally a*e7 does s!acing $or youautomatically, and unless you use the tilde character, R will ignoreyour attem!ts to add s!aces#(

a Z!m "% Zge 15ai Zge 0YYYZ$orall i

a ± b

 x ≥ 15

ai ≥ 0∀ i

Special unctions

a*e7 ty!esets s!ecial $unctions in a di^erent $ont $rom mathematical

)aria"les# *hese $unctions, such assin

,cos

, etc# are indicated ina*e7 with a "ackslash# +ere is an e%am!le that also illustrates how toty!eset an integral#

Zint0XQ2Z!iS Zsin %Yd%

∫0

2π 

sin x dx

Matrices

atrics are !resented in the array en)ironment# ?ne "egins with thestatement Z"eginQarrayS and ends with the statement ZendQarrayS#/ollowing the o!ening statement, a $ormat code is used to indicate the$ormatting o$ each column# In the e%am!le "elow, we use the codeQrrrS to indicate that each column is right =ustifed# ach row is thenentered, with cells se!arated "y the ] sym"ol, and each line &e%ce!tthe last( terminated "y ZZ#

Page 25: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 25/27

Z"eginQarraySQrrrS1 ] 2 ] D ZZG ] 5 ] ZZK ] 8 ] ZendQarrayS

1 2 3

4 5 6

7 8 9

In math te%t"ooks, matrices are o$ten surrounded "y "rackets, and areassigned to a "old$ace letter# +ere is an e%am!le

Zmath"$Q7S P Zle$t:Z"eginQarraySQrrrS

1 ] 2 ] D ZZG ] 5 ] ZZK ] 8 ] ZendQarraySZright;

 X =[1 2 3

4 5 6

7 8 9]

Math=a3 script

knitr !ro)ides sel$'contained +* code that calls a ath=a% scri!t todis!lay $ormulas# +owe)er, in order to include the scri!t in my "log!osts I took the scri!t and incor!orated it into my "logger tem!late# I$you are )iewing this !ost through syndication or an R reader, thismay not work# Eou may need to )iew this !ost on my we"site#

a'les

 *a"les can "e included using the $ollowing notation

\ B \ 6 ''' \ ''' \ ''' 1 \ ale \ Blue 2 \ /emale \ -ink

4)perlinks4 +ere is my "log nik"ear"rown#com my "log nik"ear"rown#com#

Images

+ereMs an e%am!le image

Page 26: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 26/27

Page 27: M01 Lesson 01

7/25/2019 M01 Lesson 01

http://slidepdf.com/reader/full/m01-lesson-01 27/27

4 arkdown Basics which descri"es the most commonly usedmarkdown constructs#

4 R 6ode 6hunks, which goes into more de!th on customiing the"eha)ior o$ em"edded R code#

4 R arkdown 6heat heet &-./( a 9uick guide to the most

commonly used markdown synta%, knitr o!tions, and out!ut$ormats#

4 R arkdown Re$erence Auide &-./(, a more com!rehensi)ere$erence guide to markdown, knitr, and out!ut $ormat o!tions#

4 Bi"liogra!hies and 6itations, which descri"es how to includere$erences in R arkdown documents#

4 Interacti)e .ocuments with hiny, which descri"es how to make Rarkdown documents interacti)e using hiny#

4 6om!iling Note"ooks which descri"es how to com!ile +*, -./,or ord note"ooks $rom R scri!ts#