Writing R scripts is easy. Writing good R code is hard. In this course, we'll discuss defensive programming - a set of standard techniques that will help reduce bugs and aid working in teams. We examine techniques for avoiding common errors and also how to handle the inevitable error that arises in our code. The course will conclude looking at when to make the transition from script to project to package.
In this first chapter, you'll learn what defensive programming is, and how to use existing packages for increased efficiency. You will then learn to manage the packages loaded in your environment and the potential conflicts that may arise.
Early warning systems
Programming is simpler when you get feedback on your code execution. In R, we use messages, warnings and errors to signal to keep the user informed. This chapter will discuss when and where you should use these communication tools.Early warning systems50 xpTo TRUE or not to T50 xpLet's be evil100 xpIf it weren't for those pesky kids50 xpMessage in a bottle50 xpDid you get the message?50 xpSuppressing startup messages100 xpStop being noisy!!!100 xpUsing message in practice100 xpYou have been warned50 xpMessages vs Warnings50 xpSuppressing warnings100 xpStop (right now)50 xpWarnings vs Stop50 xpUsing the stop() function100 xp
Preparing your defenses
We can avoid making mistakes using a consistent programming approach. In this chapter, we will introduce you to R best practices.Preparing your defences50 xpWhat does DRY mean?50 xpRefactoring: functions100 xpJust one comment50 xpHeader comments100 xpA little bit dotty50 xpUse a full stop in variable names50 xpAvoiding the .100 xpCoding Style50 xpImportance of consistent style50 xpStatic Code Analysis for R50 xpCode tidying100 xpMore linting100 xp
Creating a Battle Plan
Creating a script is nice, but working on a project with several scripts and assets requires structure. This final chapter will teach you good organization practices, so you can go from script to package with an optimal workflow.A battle plan50 xpThe importance of consistency50 xpGive me some space50 xpHuman readable filenames50 xpWhat date format should we use?50 xpOrganizing a project50 xpAvoiding absolute directories50 xpThe input/ directory50 xpAbsolute vs relative100 xpGraphics and output50 xpGenerating graphics50 xpGraphics/100 xpOne final work flow100 xp
Colin GillespieSee More
Assoc Prof at Newcastle University, Consultant at Jumping Rivers
Colin is the author of Efficient R Programming, published by O'Reilly media. He is an Associate Professor of Statistics at Newcastle University, UK and regularly works with Jumping Rivers to provide data science training and consultancy. He is the only person in history to move to Newcastle for better weather.