Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RStudio graphics device really slow for spatial objects on mac

I recently got a new macbook pro and am having some R graphics related problems on it. R is working insanely slowly when plotting sf objects. I found a thread that's a couple of years old on this issue (here: https://github.com/rstudio/rstudio/issues/3866), but no solution was ever proposed. For reference, RStudioGD is plotting the object >300x more slowly than pdf and it's making me crazy. Sharing the reproducible example from the link above here (though the system time numbers are mine):

<<================= copy from link above: ===========================>>

I wanted to plot the shapefile for Myanmar found here:

https://gadm.org/download_country_v3.html

library(rgdal)
library(sp)
tdir = tempdir()

get_poly = function() {
  tmp = tempfile(tmpdir = tdir)
  download.file(
    'https://biogeo.ucdavis.edu/data/gadm3.6/shp/gadm36_MMR_shp.zip',
    tmp
  )
  
  unzip(tmp, exdir = tdir)
  
  readOGR(tdir, 'gadm36_MMR_0', stringsAsFactors = FALSE)
}

Plotting this with RStudioGD is much, much slower than to e.g. pdf:

mmr = get_poly()
system.time(plot(mmr))
#    user  system elapsed 
# 128.162   0.510 129.271 
unlink(tdir, recursive = TRUE)

Restart R to clear cache/overhead and run again:

mmr = get_poly()
tpdf = tempfile(tmpdir = tdir, fileext = 'pdf')
system.time({
  pdf(tpdf)
  plot(mmr)
  dev.off()
})
#    user  system elapsed 
# 0.423   0.027   0.460 
unlink(tdir, recursive = TRUE)

So using the external device is about 300x faster... any idea?

png also takes < 1 second

<<=================== end copy from link =======================>>

I am on macOS Big Sur 11.1 RStudio version 1.3.1093

(I am having some other vague graphics-related problems that I posted about here: quartz device behaving strangely after mac update - R mac, but I am not sure if the two are related or not).

like image 855
Jake L Avatar asked Dec 01 '25 06:12

Jake L


1 Answers

I was having the same issue. It was fixed after switching to Graphics Device Backend: "AGG".

> system.time(plot(mmr))
   user  system elapsed 
  0.213   0.019   0.243

My session info:

R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6
RStudio 2021.09.0+351 "Ghost Orchid" Release (077589bcad3467ae79f318afe8641a1899a51606, 2021-09-20) for macOS
Mozilla/5.0 (Macintosh; Intel Mac OS X 11_6_0) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36
like image 146
martinolmos Avatar answered Dec 04 '25 00:12

martinolmos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!