A battle plan

Defensive R Programming

Dr. Colin Gillespie

Jumping Rivers

Starting small

Great results, can be achieved with small forces.

Sun Tzu, The Art of War

Defensive R Programming

What's in a (file)name?

  • All R scripts are stored in files
    • So filenames are important
    • Consistency in filenames is very important
      • What sort of rules could we have?

Defensive R Programming

Multiple words

Filenames often contain multiple words

For example

  • cluster-analysis.R
  • load-survival-data.R
  • plot-residuals.R
Defensive R Programming

Spaces, dashes or underscores?

Simple question. How should words be separated?

  • Space: analysis clustering.R
  • Underscores: analysis_clustering.R
  • Dashes: analysis-clustering.R

Take a second and answer these two questions

  • Which do you use?
  • What should you use?
Defensive R Programming

Spaces in filenames

  • Don't use them
  • Really, just don't

Spaces in filenames and directories are a bad idea

  • If you put the file on the web file name.R becomes file%20name.R
  • On the command line harder filenames with spaces need to be surrounded by quotes
  • Regular expressions are also more painful
  • Spotting the difference between
    • file name.R - one space
    • file name.R - two spaces is hard
Defensive R Programming

Dashes or underscores

There are a few minor problems with underscores

  • Google treats file_name as a single word
    • So searching for just file won't work
  • The regular expression character \w treats _ as a character

The same problems don't apply to dashes

Confession time:

  • I usually use underscores
  • but I'm trying to change
Defensive R Programming

Let's have some practice

Defensive R Programming

Preparing Video For Download...