3

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.

13
  • I can't replicate this on a fairly old laptop. plot(local_authorities) takes a few seconds to plot each of the five maps. Have you tried plotting subsets, eg plot(local_authorities[1:10,]) and seeing if time scales with size or maybe there's one troubling feature causing a weird loop somewhere...
    – Spacedman
    Commented Feb 28, 2022 at 10:58
  • @Spacedman thx for this quick reply. What OS is on your laptop? Key feature so far seems to be that stuff runs fine on Windows... Commented Feb 28, 2022 at 10:59
  • Good idea re plotting subsets - Ill try this and see if i can recursively identify the issue here... Commented Feb 28, 2022 at 11:00
  • Laptop is Dell XPS12 8Gb running Linux, R 4.1.0, sf version 1.0.1.
    – Spacedman
    Commented Feb 28, 2022 at 11:12
  • I'd be interested to find out if another user on a Mac could run this code without arbitrary delays Commented Feb 28, 2022 at 11:40

1 Answer 1

3

Very helpful answer from a user over on the sf() github repo in response to a bug report I'd posted. The problem is caused by poor performance by the default video device on MacOS. Longer conversation about this can be found among RStudio issues on github, along with a description of the fix, which is to modify settings in RStudio under General > Graphics to AGG. Complete shift in performance on my laptop:

> benchmark("local_authorities" = {plot(local_authorities)}, replications = 1, columns = c("test", "replications", "elapsed", "relative", "user.self", "sys.self"))
               test replications elapsed relative user.self sys.self
1 local_authorities            1   9.017        1     8.073    0.437
> 
3
  • 1
    When using an IDE like RStudio, always test issues in the R terminal to rule out that it is an issue with the IDE. This separates out R and package issues from that of an IDE, which heads off quite a bit of speculation. This is particularly important with RStudio because, it is parsing quite a bit of R before you see results. I have seen this go very wrong with S3 spatial objects and was no fault of R or the package on hand. Commented Feb 28, 2022 at 21:37
  • 1
    Thanks for this @JeffreyEvans . I did actually test in R command line and replicate there. I gather it also uses the default as is the case with RStudio. Commented Feb 28, 2022 at 21:39
  • 1
    Jeremy Kidwell, good to know. This ends up being a comment for future readers because, many users are not as savvy as you and do not know the difference between R and RStudio. It is frustrating chasing your tail over a problem that is coming from RStudio and not R. Sometimes an OP does not even mention that they are using RStudio. Now you have me wanting to try ragg under windows. Commented Feb 28, 2022 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.