and computing etc1010: data modelling · exercises on creating rmarkdown reports on the humble...
TRANSCRIPT
ETC1010:DataModellingandComputingWeekofintroduction:RmarkdownProfessorDiCook&Dr. NicholasTierneyEBS,MonashU.2019-08-02
Whatisthissong?
2/62
Recap3/62
Tra�cLightSystem
4/62
RedPostit
--
IneedahandSlowdown
GreenPostit
--
IamuptospeedIhavecompletedthething
Tra�cLightSystem
5/62
6/62
7/62
8/62
9/62
Ressentials:Ashortlist(fornow)Functionsare(mostoften)verbs,followedbywhattheywillbeappliedtoinparentheses:
do_this(to_this)do_that(to_this,to_that,with_those)
Columns(variables)indataframesareaccessedwith$:
dataframe$var_name
Packagesareinstalledwiththeinstall.packagesfunctionandloadedwiththelibraryfunction,oncepersession:
install.packages("package_name")library(package_name)
10/62
Today:Outline
WhywecareaboutReproducibilityR+markdown=RmarkdownControlingoutputandinputofrmarkdownExercisesoncreatingrmarkdownreportsonthehumbleplatypusFormupassignmentgroupsQuizReleaseassignment(latertoday)
11/62
Weareinatightspotwithreproducibility
12/62
Only6outof53landmarkresultscouldbereproduced
--Amgen,2014*
*HeardviaGarretGrolemund'sgreattalk
13/62
Anestimated75%-90%ofpreclinicalresultscannotbereproduced
--Begley,2015*
*HeardviaGarretGrolemund'sgreattalk
14/62
Estimatedannualcostofirreproducibilityforbiomedicalindustry=28BillionUSD
--Freedman,2015*
*HeardviaGarretGrolemund'sgreattalk
15/62
16/62
17/62
18/62
Sowhatcanwedoaboutit?
19/62
Reproducibilitychecklist
Near-termgoals:
Arethetablesand�guresreproduciblefromthecodeanddata?Doesthecodeactuallydowhatyouthinkitdoes?Inadditiontowhatwasdone,isitclearwhyitwasdone?(e.g.,howwereparametersettingschosen?)
20/62
Reproducibilitychecklist
Long-termgoals:
Canthecodebeusedforotherdata?Canyouextendthecodetodootherthings?
21/62
Literateprogrammingisapartialsolution
Literateprogrammingshinessomelightonthisdarkareaofscience.
AnideafromDonaldKnuthwhereyoucombineyourtextwithyourcodeoutputtocreateadocument.
Ablendofyourliterature(text),andyourprogramming(code),tocreatesomethingyoucanreadfromtoptobottom.
22/62
So
Imagineareport:
Introduction,methods,results,discussion,andconclusion,
andallthebitsofcodethatmakeeachsection.
Withrmarkdown,youcanseeallthepiecesofyourdataanalysisll t th
23/62
ll t th
Markdownasanewplayertolegibility
In2004,JohnGruber,ofdaring�reballcreatedmarkdown,asimplewaytocreatetextthatrenderedintoaHTMLwebpage.
24/62
-bulletlist-bulletlist-bulletlist bulletlist
bulletlistbulletlist
25/62
1.numberedlist2.numberedlist3.numberedlist
__bold__,**bold**,_italic_,*italic*
>quoteofsomethingprofound
1.numberedlist2.numberedlist3.numberedlist
bold,bold,italic,italic
quoteofsomethingprofound
26/62
Withverylittlemarkingup,wecancreaterichtext,thatactuallyresemblesthetextthatwewanttosee.
27/62
LearntousemarkdownInyoursmallgroups,spend�veminutesworkingthroughmarkdowntutorial.com
05:0028/62
Rmarkdownhelpscompletethesolutiontothereproducibilityproblem
Q:Howdowetakemarkdown+Rcode="literateprogrammingenvironment"
A:Rmarkdown
29/62
Rmarkdown...
Providesanenvironmentwhereyoucanwriteyourcompleteanalysis,andmarriesyourtext,andcodetogetherintoarichdocument.
Youwriteyourcodeascodechunks,putyourtextaroundthat,andthenheypresto,youhaveadocumentyoucanreproduce.
30/62
Reminder:You'vealreadyusedrmarkdown!
31/62
HowwillweuseRMarkdown?
Everyassignment+project/isanRMarkdowndocumentYou'llalwayshaveatemplateRMarkdowndocumenttostartwithTheamountofsca�oldinginthetemplatewilldecreaseoverthesemesterTheselecturenotesarecreatedusingRMarkdown(!)
32/62
Theanatomyofanrmarkdowndocument
Therearethreepartstoanrmarkdowndocument.
Metadata(YAML)Text(markdownformatting)Code(codeformatting)
DEMO33/62
Metadata:YAML(YAMLAin'tMarkupLanguage)
Themetadataofthedocumenttellsyouhowitisformed-whatthetitleis,whatdatetoput,andothercontrolinformation.
Ifyou'refamiliarwith ,thisisksimilartohowyouspecifydocumenttype,styles,fonts,options,etcinthefrontmatter/preamble.
LT XAE
34/62
Metadata:YAML
RmarkdowndocumentsuseYAMLtoprovidethemetadata.Itlookslikethis:
---title:"Anexampledocument"author:"NicholasTierney"output:html_document---
Itstartsanendswiththreedashes---,andhas�eldslikethe 35/62
f ll d
1.bulletlist2.bulletlist3.bulletlist
1.bulletlist2.bulletlist3.bulletlist
Text
Ismarkdown,aswediscussedintheearliersection,
Itprovidesasimplewaytomarkuptext
36/62
Code
Werefertocodeinanrmarkdowndocumentintwoways:
1.Codechunks,and2.Inlinecode.
37/62
Code:Codechunks
Codechunksaremarkedbythreebackticksandcurlybraceswithrinsidethem:
```{rchunk-name}#acodechunk```
38/62
abacktickisaspecialcharacteryoumightnothaveseenbefore,itistypicallylocatedunderthetildekey(~).OnUSA/Australiakeyboards,isundertheescapekey:
39/62
Code:Inlinecode
Sometimesyouwanttorunthecodeinsideasentence.Thisiscalledrunningthecode"inline".
Youmightwanttorunthecodeinlinetonamethenumberofvariablesorrowsinadatasetinasentencelike:
ThereareXXXobservationsintheairqualitydataset,andXXXvariables.
40/62
Code:Inlinecode
Youcancallcode"inline"likeso:
Thereare`rnrow(airquality)`observationsintheairqualitydataset,and`rncol(airquality)`variables.
Whichgivesyouthefollowingsentence
Thereare153observationsintheairqualitydataset,and6i bl
41/62
i bl
Code:Inlinecode
What'sgreataboutthisisthatifyourdatachangesupstream,thenyoudon'tneedtoworkoutwhereyoumentionedyourdata,youjustupdatethedocument.
42/62
YourTurn:Putittogether
Gotorstudio.cloudand
openthedocument"01-oz-atlas.Rmd"knitthedocumentChangethedatasectionatthetoptobefromadi�erentstateinsteadof"NewSouthWales"knitthedocumentagain
Howdothetextand�guresinthedocumentchange? 05:0043/62
break44/62
45/62
Code:Chunknames
Straightafterthe```{ryoucanuseatextstringtonamethechunk:
```{rread-crime-data}
```{rread-crime-data}crime<-read_csv("data/crime-data.csv")```
46/62
Code:ChunkNames
Namingcodechunkshasthreeadvantages:
1.Navigatetospeci�cchunksusingthedrop-downcodenavigatorinthebottom-leftofthescripteditor.
2.Graphicsproducedbychunksnowhaveusefulnames.3.Youcansetupnetworksofcachedchunkstoavoidre-performing
expensivecomputationsoneveryrun.
47/62
Code:Chunknames
Everychunkshouldideallyhaveaname.Namingthingsishard,butfollowtheserulesandyou'llbe�ne:
1.Onewordthatdescribestheaction(e.g.,"read")2.Onewordthatdescribesthethinginsidethecode(e.g,
"gapminder")3.Separatewordswith"-"(e.g.,read-gapminder)
48/62
Code:Chunkoptions
Youcancontrolhowthecodeisoutputbychangingthecodechunkoptionswhichfollowthetitle.
```{rread-gapminder,eval=FALSE,echo=TRUE}gap<-read_csv("gapminder.csv")```
Whatdoyouthinkthisdoes?
00:3049/62
Code:ChunkoptionsThecodechunkoptionsyouneedtoknowaboutrightnoware:
cache:TRUE/FALSE.Doyouwanttosavetheoutputofthechunksoitdoesn'thavetorunnexttime?eval:TRUE/FALSEDoyouwanttoevaluatethecode?echo:TRUE/FALSEDoyouwanttoprintthecode?include:TRUE/FALSEDoyouwanttoincludecodeoutputinthe�naloutputdocument?SettingtoFALSEmeansnothingisputintotheoutputdocument,butthecodeisstillrun.
Youcanreadmoreabouttheoptionsattheo�cialdocumentation:https://yihui.name/knitr/options/#code-evaluation 50/62
Yourturn
gotorstudio.cloud,opendocument01-oz-atlas.Rmdandchangethedocumentsothatthecodeoutputishidden,butthegraphicsareshown.(Hint:Google"rstudiormarkdowncheatsheet"forsometips!)Re-Knitthedocument.TakealookattheRMarkdownGallery.
05:0051/62
Globaloptions:Setandforget
Youcansetthedefaultchunkbehaviouronceatthetopofthe.Rmd�leusingachunklike:
knitr::opts_chunk$set(echo=FALSE,cache=TRUE)
thenyouwillonlyneedtoaddchunkoptionswhenyouhavethei l th t 'd lik t b h di� tl
52/62
i l th t 'd lik t b h di� tl
Yourturn
Gotoyour01-oz-atlas.Rmddocumentonrstudio.cloudandchangetheglobalsettingsatthetopofthermarkdowndocumenttoecho=FALSE,andcache=TRUEUpdatetheothercodechunksbyremovingthecodechunkoptions.
03:0053/62
DEMO
Themanydi�erentoutputsofrmarkdown
54/62
Yourturn:Di�erenttypesofdocuments
1.ChangetheoutputofyourcurrentRMarkdown�letoproduceaWorddocument.Nowtrytoproducepdf-thismaynotwork!That'sOK,wedo'ntneeditrightnow.
2.CreateanewdocumentthatwillproduceaslideshowFile>NewRMarkdown>Presentation
3.Createa�exdashboarddocument-seethisoptionintheFile>NewRMarkdown>Fromtemplate
list. 55/62
YourTurn:Makingthegroups
Wearegoingtosetupthegroupsfordoingassignmentwork.
1.Chooseaquotefromthebag.2.Findtheotherpeopleintheclasswiththesamequoteasyou3.Grabyourgearandclaimatabletoworktogetherat.
56/62
YourTurn:Askyourteammatesthesequestions:
1.Whatisonefoodyou'dneverwanttotasteagain?2.Ifyouwereacomicstripcharacter,whowouldyoubeandwhy?
LASTLY,comeupwithanameforyourteamandtellthistoatutor,alongwiththenamesofmembersoftheteam.
05:0057/62
YourTurn
Gotorstudio.cloudtooz-atlas-final.RmdReadthroughthedocumentandaddtextwherepromptedtolearnmoreabouttheAustraliannativeplatypus!
58/62
Recap:
ThereisaReproducibilityCrisisrmarkdown=YAML+text+codermarkdownhasmanydi�erentoutputtypesPlatypusareinteresting!Assignmentwillbeannouncedlatertoday
59/62
Learningmore:
RMarkdowncheatsheetandMarkdownQuickReference(Help->MarkdownQuickReference)handy,we'llrefertoitoftenasthecourseprogresses
60/62
Labquiz
TakethequizfortodayfromED.
61/62
Shareandsharealike
ThisworkislicensedunderaCreativeCommonsAttribution-NonCommercial-ShareAlike4.0InternationalLicense.
62/62