7 Functions & packages

7.1 R Functions

If you find yourself copying and pasting the same code several times, that’s a good indication that you should probably consider creating a function. Writing functions is an initial investment of time and effort, that pays off down the line, by making it easier to read and maintain scripts.

The place to start understanding the purpose of functions and how to write R functions, is the relevant chapter in Hadley’s R for Data Science.

7.1.1 Lazy & tidy evaluation

There are a couple of concepts to be aware of when writing R functions:

  1. Lazy evaluation

  2. Tidy evaluation

Tidy evaluation in particular, I’m not comfortable enough with yet to try and explain. I’m getting better at using it, but I don’t fully understand it. Hadley’s Advanced R book is where you should look for an explanation:

7.1.2 Sourcing files

When you need access to functions across different scripts in your project, you can put your functions in a separate R script and include them in a script using the source function. The source function can be used with a relative path (i.e. to your working directory) or an absolute path. It can also be used with a URL, for example to a (Raw) file on GitHub.

7.2 R Packages

7.2.1 Writing packages

If you find that your functions are useful across different projects then you could include them in an R package. Writing R packages isn’t conceptually difficult, it’s more a case of becoming familiar with the R package conventions and documenting your code more than you usually do.

I’ve written a couple of small R packages called popsheff and boundsheff. Instead of functions, these contain population and boundary data for Sheffield. The resources I relied on to understand how to create the packages and what an R package contains include:

Resources I intend to use include:

7.2.2 Installing packages

Packages are typically installed in to your library (a designated folder on a local drive) using the install.packages function. Packages are loaded into an R session from your library using the library function.

install.packages is usually used to install packages hosted on CRAN. Hosting a package on CRAN involves a verification process, quality check, reviews etc. I’ve not done that for the popsheff and boundsheff packages, so they have to be installed a different way, either using:

  1. RStudio and the Tools > Install packages… menu item to load the Windows binary (zip) file from S:/BI Team/ShareToAll/RPackages if you work for Sheffield City Council.

  2. GitHub via the devtools R package and its install_github function.

#install.packages("devtools")
devtools::install_github("scc-pi/popsheff")