9.1 Introduction

A defining feature of R is the way you interact with it:you type commands and hit Enter (or Ctrl+Enter if writing code in the source editor in RStudio) to execute them interactively.This way of interacting with the computer is called a command-line interface (CLI) (see definition in the note below).CLIs are not unique to R.41In dedicated GIS packages, by contrast, the emphasis tends to be on the graphical user interface (GUI).You can interact with GRASS, QGIS, SAGA and gvSIG from system terminals and embedded CLIs such as the Python Console in QGIS, but ‘pointing and clicking’ is the norm.This means many GIS users miss out on the advantages of the command-line according to Gary Sherman, creator of QGIS (Sherman 2008):

With the advent of ‘modern’ GIS software, most people want to point andclick their way through life. That’s good, but there is a tremendous amountof flexibility and power waiting for you with the command line. Many timesyou can do something on the command line in a fraction of the time youcan do it with a GUI.

The ‘CLI vs GUI’ debate can be adversial but it does not have to be; both options can be used interchangeably, depending on the task at hand and the user’s skillset.42The advantages of a good CLI such as that provided by R (and enhanced by IDEs such as RStudio) are numerous.A good CLI

  • Facilitates the automation of repetitive tasks.
  • Enables transparency and reproducibility, the backbone of good scientific practice and data science.
  • Encourages software development by providing tools to modify existing functions and implement new ones.
  • Helps develop future-proof programming skills which are in high demand in many disciplines and industries.
  • Is user-friendly and fast, allowing an efficient workflow.
    On the other hand, GUI-based GIS systems (particularly QGIS) are also advantageous.A good GIS GUI

  • Has a ‘shallow’ learning curve meaning geographic data can be explored and visualized without hours of learning a new language.

  • Provides excellent support for ‘digitizing’ (creating new vector datasets), including trace, snap and topological tools.43
  • Enables georeferencing (matching raster images to existing maps) with ground control points and orthorectification.
  • Supports stereoscopic mapping (e.g., LiDAR and structure from motion).
  • Provides access to spatial database management systems with object-oriented relational data models, topology and fast (spatial) querying.
    Another advantage of dedicated GISs is that they provide access to hundreds of ‘geoalgorithms’ (computational recipes to solve geographic problems — see Chapter 10).Many of these are unavailable from the R command line, except via ‘GIS bridges’, the topic of (and motivation for) this chapter.44

A command-line interface is a means of interacting with computer programs in which the user issues commands via successive lines of text (command lines).bash in Linux and PowerShell in Windows are common examples.CLIs can be augmented with IDEs such as RStudio for R, which provides code auto-completion and other features to improve the user experience.

R originated as an interface language.Its predecessor S provided access to statistical algorithms in other languages (particularly FORTRAN), but from an intuitive read-evaluate-print loop (REPL) (Chambers 2016).R continues this tradition with interfaces to numerous languages, notably C++, as described in Chapter 1.R was not designed as a GIS.However, its ability to interface with dedicated GISs gives it astonishing geospatial capabilities.R is well known as a statistical programming language, but many people are unaware of its ability to replicate GIS workflows, with the additional benefits of a (relatively) consistent CLI.Furthermore, R outperforms GISs in some areas of geocomputation, including interactive/animated map making (see Chapter 8) and spatial statistical modeling (see Chapter 11).This chapter focuses on ‘bridges’ to three mature open source GIS products (see Table 9.1): QGIS (via the package RQGIS; Section 9.2), SAGA (via RSAGA; Section 9.3) and GRASS (via rgrass7; Section 9.4).Though not covered here, it is worth being aware of the interface to ArcGIS, a proprietary and very popular GIS software, via RPyGeo.^[By the way, it is also possible to use R from within Desktop GIS software packages.The so-called R-ArcGIS bridge (see https://github.com/R-ArcGIS/r-bridge) allows R to be used from within ArcGIS.One can also use R scripts from within QGIS (see https://docs.qgis.org/2.18/en/docs/training_manual/processing/r_intro.html).Finally, it is also possible to use R from the GRASS GIS command line (see https://grasswiki.osgeo.org/wiki/R_statistics/rgrass7).To complement the R-GIS bridges, the chapter ends with a very brief introduction to interfaces to spatial libraries (Section 9.6.1) and spatial databases (Section 9.6.2).

Table 9.1: Comparison between three open-source GIS. Hybrid refers to the support of vector and raster operations.
GISFirst releaseNo. functionsSupport
GRASS1984>500hybrid
QGIS2002>1000hybrid
SAGA2004>600hybrid