Open source book: Geocomputation with R
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "images/"
)
is_online = curl::has_internet()
This repository hosts the code underlying Geocomputation with R, a book by Robin Lovelace, Jakub Nowosad, and Jannes Muenchow.
If you find the contents useful, please cite it as follows:
Lovelace, Robin, Jakub Nowosad and Jannes Muenchow (2025). Geocomputation with R. The R Series. CRC Press.
To learn more about the second edition of the book, see the “Second edition of Geocomputation with R is complete” blog post.
The first version of the book has been published by CRC Press in the R Series and can be viewed online at bookdown.org.
Read the latest version at r.geocompx.org.
Contributions are very welcome.
We encourage contributions on any part of the book, including:
See our-style.md for the book’s style.
contributors = source("code/list-contributors.R")[[1]]
# save for future reference:
readr::write_csv(contributors, "extdata/contributors.csv")
# table view:
# knitr::kable(contributors, caption = "Contributors to Geocomputation with R")
# text view
c_txt = contributors$name
c_url = contributors$link
c_rmd = paste0("[", c_txt, "](", c_url, ")")
contributors_text = paste0(c_rmd, collapse = ", ")
Many thanks to all contributors to the book so far via GitHub (this list will update automatically): r contributors_text
.
During the project we aim to contribute ‘upstream’ to the packages that make geocomputation with R possible.
This impact is recorded in our-impact.csv
.
The recommended way to get the source code underlying Geocomputation with R on your computer is by cloning the repo.
You can can that on any computer with Git installed with the following command:
git clone https://github.com/geocompx/geocompr.git
An alternative approach, which we recommend for people who want to contribute to open source projects hosted on GitHub, is to install the gh
CLI tool.
From there cloning a fork of the source code, that you can change and share (including with Pull Requests to improve the book), can be done with the following command:
gh repo fork geocompx/geocompr # (gh repo clone geocompx/geocompr # also works)
Both of those methods require you to have Git installed.
If not, you can download the book’s source code from the URL https://github.com/geocompx/geocompr/archive/refs/heads/main.zip .
Download/unzip the source code from the R command line to increase reproducibility and reduce time spent clicking around:
#| eval=FALSE
u = "https://github.com/geocompx/geocompr/archive/refs/heads/main.zip"
f = basename(u)
download.file(u, f) # download the file
unzip(f) # unzip it
file.rename(f, "geocompr") # rename the directory
rstudioapi::openProject("geococompr") # or open the folder in vscode / other IDE
To ease reproducibility, we created the geocompkg
package.
Install it with the following commands:
#| eval=FALSE
install.packages("remotes")
# To reproduce the first Part (chapters 1 to 8):
install.packages("geocompkg", repos = c("https://geocompr.r-universe.dev", "https://cloud.r-project.org"), dependencies = TRUE, force = TRUE)
Installing geocompkg
will also install core packages required for reproducing Part I of the book (chapters 1 to 8).
Note: you may also need to install system dependencies if you’re running Linux (recommended) or Mac operating systems.
You also need to have the remotes package installed:
To reproduce book in its entirety, run the following command (which installs additional ‘Suggests’ packages, this may take some time to run!):
#| eval=FALSE
# Install packages to fully reproduce book (may take several minutes):
options(repos = c(
geocompx = "https://geocompx.r-universe.dev",
cran = "https://cloud.r-project.org/"
))
# From geocompx.r-universe.dev (recommended):
install.packages("geocompkg", dependencies = TRUE)
# Alternatively from GitHub:
remotes::install_github("geocompx/geocompkg", dependencies = TRUE)
You need a recent version of the GDAL, GEOS, PROJ and udunits libraries installed for this to work on Mac and Linux.
See the sf package’s README for information on that.
After the dependencies have been installed you should be able to build and view a local version the book with:
#| eval=FALSE
# Change this depending on where you have the book code stored:
rstudioapi::openProject("~/Downloads/geocompr")
# or code /location/of/geocompr in the system terminal
# or cd /location/of/geocompr then R in the system terminal, then:
bookdown::render_book("index.Rmd") # to build the book
browseURL("_book/index.html") # to view it
# Or, to serve a live preview the book and observe impact of changes:
bookdown::serve_book(".")
#| eval=FALSE
geocompkg:::generate_chapter_code()
A great feature of VS Code is devcontainers, which allow you to develop in an isolated Docker container.
If you have VS Code and the necessary dependencies installed on your computer, you can build Geocomputation with R in a devcontainer as shown below (see #873 for details):
For many people the quickest way to get started with Geocomputation with R is in your web browser via Binder.
To see an interactive RStudio Server instance click on the following button, which will open mybinder.org with an R installation that has all the dependencies needed to reproduce the book:
You can also have a play with the repository in RStudio Cloud by clicking on this link (requires log-in):
See the geocompx/docker repository for details.
To reduce the book’s dependencies, scripts to be run infrequently to generate input for the book are run on creation of this README.
The additional packages required for this can be installed as follows:
source("code/extra-pkgs.R")
With these additional dependencies installed, you should be able to run the following scripts, which create content for the book, that we’ve removed from the main book build to reduce package dependencies and the book’s build time:
source("code/01-cranlogs.R")
source("code/sf-revdep.R")
source("code/09-urban-animation.R")
source("code/09-map-pkgs.R")
Note: the .Rproj
file is configured to build a website not a single page.
To reproduce this README use the following command:
rmarkdown::render("README.Rmd", output_format = "github_document", output_file = "README.md")
# We aim to make every script in the `code` folder reproducible.
# To check they can all be reproduced run the following:
# Aim: test reproducibility of scripts
script_names = list.files("code", full.names = T)
avoid = "pkgs|anim|us|saga|sliver|tsp|parti|polycent|cv|svm|data|location|eco|rf|cran|hex"
dontrun = grepl(avoid, script_names)
script_names = script_names[!dontrun]
counter = 0
for(i in script_names[45:length(script_names)]) {
counter = counter + 1
print(paste0("Script number ", counter, ": ", i))
source(i)
}
# source("code/generate-chapter-code.R")
book_stats = readr::read_csv("extdata/word-count-time.csv",
col_types = ("iiDd"))
# to prevent excessive chapter count
if (Sys.Date() > max(book_stats$date) + 5) {
book_stats_new = geocompkg:::generate_book_stats()
book_stats = bind_rows(book_stats, book_stats_new)
readr::write_csv(book_stats, "extdata/word-count-time.csv")
}
book_stats = dplyr::filter(book_stats, chapter <= 15)
library(ggplot2)
book_stats$chapter = formatC(book_stats$chapter, width = 2, format = "d", flag = "0")
book_stats$chapter = fct_rev(as.factor(book_stats$chapter))
book_stats$n_pages = book_stats$n_words / 300
ggplot(book_stats) +
geom_area(aes(date, n_pages, fill = chapter), position = "stack") +
ylab("Estimated number of pages") +
xlab("Date") +
scale_x_date(date_breaks = "2 month",
limits = c(min(book_stats$date), as.Date("2018-10-01")),
date_labels = "%b %Y") +
coord_cartesian(ylim = c(0, 350))
The main packages used in this book are cited from packages.bib
.
Other citations are stored online using Zotero.
If you would like to add to the references, please use Zotero, join the open group add your citation to the open geocompr library.
We use the following citation key format:
[auth:lower]_[veryshorttitle:lower]_[year]
This can be set from inside Zotero desktop with the Better Bibtex plugin installed (see github.com/retorquere/zotero-better-bibtex) by selecting the following menu options (with the shortcut Alt+E
followed by N
), and as illustrated in the figure below:
Edit > Preferences > Better Bibtex
Zotero settings: these are useful if you want to add references.
When you export the citations as a .bib file from Zotero, use the Better BibTex
(not BibLaTeX
) format.
We use Zotero because it is a powerful open source reference manager that integrates well with citation tools in VS Code and RStudio.