A battle plan D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers
Starting small Great res u lts , can be achie v ed w ith small forces . S u n T zu, The Art of War DEFENSIVE R PROGRAMMING
What ' s in a ( file ) name ? All R scripts are stored in � les So � lenames are important Consistenc y in � lenames is v er y important What sort of r u les co u ld w e ha v e ? DEFENSIVE R PROGRAMMING
M u ltiple w ords Filenames o � en contain m u ltiple w ords For e x ample cluster-analysis.R load-survival-data.R plot-residuals.R DEFENSIVE R PROGRAMMING
Spaces , dashes or u nderscores ? Simple q u estion . Ho w sho u ld w ords be separated ? Space : analysis clustering.R Underscores : analysis_clustering.R Dashes : analysis-clustering.R Take a second and ans w er these t w o q u estions Which do y o u u se ? What sho u ld y o u u se ? DEFENSIVE R PROGRAMMING
Spaces in filenames Don ' t u se them Reall y, j u st don ' t Spaces in � lenames and directories are a bad idea If y o u p u t the � le on the w eb file name.R becomes file%20name.R On the command line harder � lenames w ith spaces need to be s u rro u nded b y q u otes Reg u lar e x pressions are also more painf u l Spo � ing the di � erence bet w een file name.R - one space file name.R - t w o spaces is hard DEFENSIVE R PROGRAMMING
Dashes or u nderscores There are a fe w minor problems w ith u nderscores Google treats file_name as a single w ord So searching for j u st file w on ' t w ork The reg u lar e x pression character \w treats _ as a character The same problems don ' t appl y to dashes Confession time : I u s u all y u se u nderscores b u t I ' m tr y ing to change DEFENSIVE R PROGRAMMING
Let ' s ha v e some practice D E FE N SIVE R P R OG R AMMIN G
H u man Readable Filenames D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers
The h u mble sl u g URL sl u gs are the end part of a w eb address Which URL do y o u prefer ? www. datacamp . com / co u rses / co u rse 1963. htm DEFENSIVE R PROGRAMMING
We can learn from sl u gs Use sensible names ac.R or analysis-clustering.R 1.R or loading.R Be consistent Use the same � le e x tension - .R Al w a y s lo w er case DEFENSIVE R PROGRAMMING
Dates - w hat do w e w ant ? Unambig u o u s So not 01/02/032 Sortable in a � le s y stem DEFENSIVE R PROGRAMMING
Dates - ISO 8601 Dates sho u ld be YYYY - MM - DD All dates are no w in an ob v io u s and nat u ral order Sorting j u st w orks ! 2017-01-02 2018-01-01 2018-01-02 DEFENSIVE R PROGRAMMING
N u mbers are good For this co u rse , I created directories called chapter01 chapter02 Simple , y et e � ecti v e DEFENSIVE R PROGRAMMING
Let ' s practice ! D E FE N SIVE R P R OG R AMMIN G
Organi z ing a project D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers
It starts w ith something small All R anal y ses start w ith a li � le code , b u t then 1 line becomes 10 1 imported package becomes 5 1 � le becomes a mess DEFENSIVE R PROGRAMMING
Project Set -u p E v er y project I w ork on Has its o w n director y Has a sensible name The director y name gi v es the conte x t of the scripts DEFENSIVE R PROGRAMMING
Director y: inp u t / This director y contains data , t y picall y csv & e x cel � les No R code Data is onl y edited in R DEFENSIVE R PROGRAMMING
Director y: R / All R code li v es in this director y Notice The director y isn ' t R _ anal y sis R _ code R _ s u r v i v al j u st plain R / DEFENSIVE R PROGRAMMING
Director y: R / In this director y, I al w a y s ha v e a � le called load.R This � le loads the data from input/ E v er y project I 'v e w orked has a similar str u ct u re I can gi v e y o u an y project and y o u can load the data DEFENSIVE R PROGRAMMING
The load . R file All paths are relati v e battles <- read_csv("input/battles.csv") foes <- read_xlsx("input/foes.xlsx") M y code is portable DEFENSIVE R PROGRAMMING
Other R files Remember , all R � les li v e in the R director y! clean.R - for cleaning y o u r data function.R - an y helper f u nctions analysis.R - the act u al anal y sis Standard names u sed in e v er y project DEFENSIVE R PROGRAMMING
Yo u r t u rn D E FE N SIVE R P R OG R AMMIN G
Graphics and O u tp u t D E FE N SIVE R P R OG R AMMIN G Dr . Colin Gillespie J u mping Ri v ers
Project o v er v ie w 1 So far w e ha v e enco u ntered the base project director y DEFENSIVE R PROGRAMMING
Project o v er v ie w 2 So far w e ha v e enco u ntered the base project director y input/ for data � les DEFENSIVE R PROGRAMMING
Project o v er v ie w 3 So far w e ha v e enco u ntered the base project director y input/ for data � les R/ for R scripts DEFENSIVE R PROGRAMMING
Project o v er v ie w 4 So far w e ha v e enco u ntered the base project director y R/ for R scripts input/ for data sets In this last v ideo , w e ' ll look at output/ for o u tp u t generated data � les DEFENSIVE R PROGRAMMING
Project o v er v ie w 5 So far w e ha v e enco u ntered the base project director y R/ for R scripts input/ for data sets In this last v ideo , w e ' ll look at output/ for o u tp u t generated data � les graphics/ for generated plots DEFENSIVE R PROGRAMMING
The difference The scripts in the R/ director y create the contents of output/ & graphics/ So in theor y, w e can delete output/ & graphics/ and not cr y DEFENSIVE R PROGRAMMING
The graphics / director y This director y j u st contains graphics ! In m y R/ director y I ha v e imaginati v el y named script graphics.R that generates all graphics Make s u re to u se relati v e paths ! DEFENSIVE R PROGRAMMING
The o u tp u t / director y This director y contains o u tp u t For e x ample List of signi � cant v ariables , perhaps p -v al u e Data for the ne x t anal y sis Personall y, I t y picall y don ' t u se this director y DEFENSIVE R PROGRAMMING
Let ' s tr y it D E FE N SIVE R P R OG R AMMIN G
Recommend
More recommend