Chapter 1 Introduction

This document includes all the code used in the book. The code is presented in the same order, in the same the sections and sub-sections in which it is found in the hard copy of the book but without any of the commentary. The code for each chapter allows the reader to copy and paste it into the R/RStudio.

When the book was written, all of the code in the book was correct. However, R and its packages are occasionally updated. In most cases this is not problematic as the update almost always extends the functionality of the package without affecting the original code. In a few instances, specific packages are completely rewritten without backward compatibility.

These pages will always be updated to handle these changes (well perhaps until the 3rd edition!) and note that there is no code that generates any outputs in the Introduction chapter.

The full book can be found at the links below:

Please contact us with any questions or suggestions:

Addendum September 2022

The GISTools package (introduced in Chapter 2) is in the process of being updated. To install it as a temporary fix there are 2 options:

  1. Run the code below.
# Download package tarball from CRAN archive
url <- "https://cran.r-project.org/src/contrib/Archive/GISTools/GISTools_0.7-4.tar.gz"
pkgFile <- "GISTools_0.7-4.tar.gz"
download.file(url = url, destfile = pkgFile)

# Install dependencies list in the DESCRIPTION file
install.packages(c("maptools", "sp", "RColorBrewer", "MASS", "rgeos"))

# Install package
install.packages(pkgs=pkgFile, type="source", repos=NULL)

# Delete package tarball
unlink(pkgFile)

# Load the package
library(GISTools)
  1. Or, if you get install errors due to your computer environment (e.g. Microsoft Windows with UNC paths) do this manually.
  • Install dependencies listed in the DESCRIPTION file
install.packages(c("maptools", "sp", "RColorBrewer", "MASS", "rgeos"))
  • Manually download ‘GISTools_0.7-4.tar.gz’ from https://cran.r-project.org/src/contrib/Archive/GISTools/

  • In RStudio go to Tools > Install Packages > Install from select “Package Archive File (.zip; .tar.gz)” then browse and select the manually downloaded ‘GISTools_0.7-4.tar.gz’ and then select Install

  • Load the package

library(GISTools)