From writing R scripts to functions and packages

Observations on the journey with reference to the stats19 R package

Robin Lovelace, Head of Data and Digital, Active Travel England
Coffee and Coding presentation for the Department for Transport’s Coding and Reproducible Analysis Network

2023-07-21

Introduction

Abstract

I’ll outline the benefits of moving from writing code as one-off scripts to be run once, and only once, to generalising your code by creating functions and packages. This will be based on his own experience going from an academic script writing (scripts that may or may not run more than once, with limited reproducibility) to becoming an R package developer. For background, please check out the stats19 package and associated documentation including the vignettes (articles) at https://docs.ropensci.org/stats19/ and the introductory textbook on analysing road crash data with R developed for the RAC foundation (ideal for people early on their R/data science journeys): https://github.com/ITSLeeds/rrsrr

What is this session?

Coffee & Coding is part of the wider CRAN network provides a regular opportunity for people within DfT who are interested in coding to share skills and knowledge, and showcase coding projects that they’re working on. See the CRAN Teams Channel files for past talks and code!

How do I get on the mailing list?

Contact ITFP to be added to (_DfT_CClist@dft.gov.uk) or check Microsoft Teams CRAN channel directly.

Presenters

If you’d like to present get in touch with Francesca Bryden or Dina Haque

Introduction to me

And ATE’s Data and Digital team

Beginning: reinventing the wheel

Wheel 1: EDA of bike crashes in West Yorkshire

Geographic analysis of STATS19 data (2016)

Reproducible code

How reproducible?

Hard-coded file paths

Source: whereWards.R

Random CSV files

Source: preliminaries.R

Packaging code

The stats19 R package

Peer reviewed R package on CRAN: Work with Open Road Traffic Casualty Data from Great Britain • stats19 (ropensci.org)

What’s in the box?

Live demo

Try in the devcontainer:

Source: acteng/from-scripts-to-packages: Slides for talk at Coding and Reproducible Analysis Network. Coffee and Coding! (github.com)

Live demo!

Appendix: how I made these slides