Problem:
When I load in any of the shapefile datasets from Census data explorer plots are brutally slow. I suspect this is an issue specific to running R / RStudio on MacOS.
For replication:
require(sf)
download.file("https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_dist_lyr_2011.zip", destfile = "data/gis/infuse_dist_lyr_2011.zip")
unzip("infuse_dist_lyr_2011.zip", exdir = "data/gis")
local_authorities <- st_read("data/gis/infuse_dist_lyr_2011.shp")
plot(local_authorities)
I'm running MacOS 11.6, R-4.1.2-arm64 and RStudio 2021.09.1+372. I've replicated this problem using R command line and in RStudio, using tmap() and base R graphics. I've also tried using both sf() objects and sp(). Plots take more than 30 minutes, potentially several hours across any of the configurations I've tried.
Following helpful advice from @Spacedman, I've run some subsets with timers on. Code I used:
require(sf)
local_authorities <- st_read("data/gis/infuse_dist_lyr_2011.shp")
benchmark("row1" = {
plot(local_authorities[1,])},
benchmark("row2" = {
plot(local_authorities[2,])},
benchmark("row3" = {
plot(local_authorities[3,])},
benchmark("row4" = {
plot(local_authorities[4,])},
replications = 1, columns = c("test", "replications", "elapsed", "relative", "user.self", "sys.self"))
Results are the following:
test replications elapsed relative user.self sys.self
1 row1 1 0.134 1 0.121 0.006
1 row2 1 964.725 1 961.112 2.294
1 row3 1 0.358 1 0.333 0.015
1 row4 1 0.136 1 0.124 0.008
1 row5 1 0.578 1 0.557 0.012
1 row6 1 0.245 1 0.237 0.004
1 row7 1 8.603 1 8.55 0.041
1 row8 1 0.776 1 0.757 0.011
1 row9 1 0.471 1 0.45 0.01
1 row10 1 0.174 1 0.167 0.003
1 row11 1 83.648 1 83.26 0.246
Note: I've run replications on 1, 3, 4 and 11 and confirmed that execution time remains relatively stable (e.g. short or long depending) so it does seems to be something in the data here.
A bit more digging into each individual shape in this shapefile seems to indicate that the struggle here relates to the number of parts within each individual MULTIPOLYGON. So row 2 has whereas Shetland, row 244 has 549 parts row 387, has 521 and both lag in similar ways.
I'm a bit out of my depth here in terms of identifying faults within individual polygons, but it seems to be the case that (as suggested some specific shapes are tripping this up, by my testing of the first four, geo_codes: W06000016 and E07000138 (1 and 4) are fine, whereas S12000013 (2) is causing problems.
Happy to try testing if someone can steer me in the right direction.
plot(local_authorities)
takes a few seconds to plot each of the five maps. Have you tried plotting subsets, egplot(local_authorities[1:10,])
and seeing if time scales with size or maybe there's one troubling feature causing a weird loop somewhere...