options(htmltools.dir.version = FALSE)
library(RefManageR)
BibOptions(check.entries = FALSE, 
           bib.style = "authoryear", 
           cite.style = 'alphabetic', 
           style = "markdown",
           first.inits = FALSE,
           hyperlink = FALSE, 
           dashed = FALSE)
my_bib = ReadBib("refs-geostat.bib", check = FALSE)

layout: true background-image: url(xaringan_stuff/img/r_geocomp_background.png) background-size: cover


Aim

install.packages("tidyverse")

sf (first release in 2016) implements the open standard data model simple features. Get sf with:

install.packages("sf")

The workshop also uses a dataset from the spData package, which can be installed with:

install.packages("spData")

For more on this see: github.com/Robinlovelace/geocompr.


Why R? (source: stackoverflow.blog)

knitr::include_graphics("https://stackoverflow.blog/wp-content/uploads/2017/10/tag_growth_scatter-1-771x675.png")

Introduction to the tidyverse

  • Each variable forms a column.
  • Each observation forms a row.
  • Each type of observational unit forms a table

background-image: url("https://pbs.twimg.com/media/CvzEQcfWIAAIs-N.jpg") background-size: cover


Enter sf

That's the topic of this workshop


background-image: url("https://media1.giphy.com/media/Hw5LkPYy9yfVS/giphy.gif")


Geocomputation with R

--

--

--


Attaching the packages

library(sf)
library(raster)
library(spData)

The structure of spatial data in sf

us_states[1:3, 1:2]

Spatial data and the tidyverse

library(tidyverse)

Data manipulation in the tidyverse (dplyr)

states1 = us_states %>%
  slice(1)
states2 = us_states %>% 
  filter(grepl(pattern = "^A", x = NAME)) %>% 
  top_n(n = 2, wt = total_pop_15)

Tidyverse pitfall example: binding rows

rbind(states1, states2)     # works
bind_rows(states1, states2) # fails
Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in get1index

Work-around (see article in geocompr.github.io/geocompkg for more pitfalls):

us_data = st_set_geometry(us_states, NULL)
d = bind_rows(us_data, us_data)
d_sf = st_sf(d, geometry = c(us_states$geom, us_states$geom))

Making maps with R


Now over to you


Final thing

A group of us are thinking about starting-up the Leeds R Users group.

Email: r.users.leeds@gmail.com



geocompr/geocompkg documentation built on July 5, 2025, 2:35 a.m.