1.2 Why use R for geocomputation?

Early geographers used a variety of tools including barometers, compasses and sextants to advance knowledge about the world (Wulf 2015).It was only with the invention of the marine chronometer in 1761 that it became possible to calculate longitude at sea, enabling ships to take more direct routes.

Nowadays such lack of geographic data is hard to imagine.Every smartphone has a global positioning (GPS) receiver and a multitude of sensors on devices ranging from satellites and semi-autonomous vehicles to citizen scientists incessantly measure every part of the world.The rate of data produced is overwhelming.An autonomous vehicle, for example, can generate 100 GB of data per day (The Economist 2016).Remote sensing data from satellites has become too large to analyze the corresponding data with a single computer, leading to initiatives such as OpenEO.

This ‘geodata revolution’ drives demand for high performance computer hardware and efficient, scalable software to handle and extract signal from the noise, to understand and perhaps change the world.Spatial databases enable storage and generation of manageable subsets from the vast geographic data stores, making interfaces for gaining knowledge from them vital tools for the future.R is one such tool, with advanced analysis, modeling and visualization capabilities.In this context the focus of the book is not on the language itself (see Wickham 2014a).Instead we use R as a ‘tool for the trade’ for understanding the world, similar to Humboldt’s use of tools to gain a deep understanding of nature in all its complexity and interconnections (see Wulf 2015).Although programming can seem like a reductionist activity, the aim is to teach geocomputation with R not only for fun, but for understanding the world.

R is a multi-platform, open source language and environment for statistical computing and graphics (r-project.org/).With a wide range of packages, R also supports advanced geospatial statistics, modeling and visualization.New integrated development environments (IDEs) such as RStudio have made R more user-friendly for many, easing map making with a panel dedicated to interactive visualization.

At its core, R is an object-oriented, functional programming language(Wickham 2014a), and was specifically designed as an interactive interface to other software (Chambers 2016).The latter also includes many ‘bridges’ to a treasure trove of GIS software, ‘geolibraries’ and functions (see Chapter 9).It is thus ideal for quickly creating ‘geo-tools’, without needing to master lower level languages (compared to R) such as C, FORTRAN or Java (see Section 1.3).This can feel like breaking free from the metaphorical ‘glass ceiling’ imposed by GUI-based or proprietary geographic information systems (see Table 1.1 for a definition of GUI).Furthermore, R facilitates access to other languages:the packages Rcpp and reticulate enable access to C++ and Python code, for example.This means R can be used as a ‘bridge’ to a wide range of geospatial programs (see Section 1.3).

Another example showing R’s flexibility and evolving geographic capabilities is interactive map making.As we’ll see in Chapter 8, the statement that R has “limited interactive [plotting] facilities” (Bivand, Pebesma, and Gómez-Rubio 2013) is no longer true.This is demonstrated by the following code chunk, which creates Figure 1.1 (the functions that generate the plot are covered in Section 8.4).

  1. library(leaflet)
  2. popup = c("Robin", "Jakub", "Jannes")
  3. leaflet() %>%
  4. addProviderTiles("NASAGIBS.ViirsEarthAtNight2012") %>%
  5. addMarkers(lng = c(-3, 23, 11),
  6. lat = c(52, 53, 49),
  7. popup = popup)

Figure 1.1: The blue markers indicate where the authors are from. The basemap is a tiled image of the Earth at night provided by NASA. Interact with the online version at geocompr.robinlovelace.net, for example by zooming in and clicking on the popups.

It would have been difficult to produce Figure 1.1 using R a few years ago, let alone as an interactive map.This illustrates R’s flexibility and how, thanks to developments such as knitr and leaflet, it can be used as an interface to other software, a theme that will recur throughout this book.The use of R code, therefore, enables teaching geocomputation with reference to reproducible examples such as that provided in Figure 1.1 rather than abstract concepts.