Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot(SpatialPolygonsDataFrame) is insanely slow for RStudioGD #3866

Open
MichaelChirico opened this issue Nov 11, 2018 · 15 comments
Open

plot(SpatialPolygonsDataFrame) is insanely slow for RStudioGD #3866

MichaelChirico opened this issue Nov 11, 2018 · 15 comments

Comments

@MichaelChirico
Copy link
Contributor

MichaelChirico commented Nov 11, 2018

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 
# 170.430   0.434 171.086 
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.513   0.022   0.539 
unlink(tdir, recursive = TRUE)

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

png also takes < 1 second

RStudio: Version 1.1.456
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] grab_0.1.8

loaded via a namespace (and not attached):
 [1] OpenStreetMap_0.3.3 Rcpp_1.0.0          pillar_1.3.0        compiler_3.5.1      plyr_1.8.4         
 [6] bindr_0.1.1         RJDBC_0.2-7.1       tools_3.5.1         jsonlite_1.5.9000   tibble_1.4.2       
[11] gtable_0.2.0        lattice_0.20-38     pkgconfig_2.0.2     rlang_0.3.0.1       DBI_1.0.0          
[16] rstudioapi_0.8      yaml_2.2.0          rgdal_1.3-6         bindrcpp_0.2.2      rJava_0.9-10       
[21] dplyr_0.7.8         raster_2.8-4        rgeos_0.4-2         grid_3.5.1          tidyselect_0.2.5   
[26] glue_1.3.0          data.table_1.11.9   R6_2.3.0            geohash_0.3.0       sp_1.3-1           
[31] SparkR_2.4.0        purrr_0.2.5         ggplot2_3.1.0       magrittr_1.5        scales_1.0.0       
[36] codetools_0.2-15    assertthat_0.2.0    colorspace_1.3-2    lazyeval_0.2.1      munsell_0.5.0      
[41] crayon_1.3.4
@pascaloettli
Copy link

Could kindly provide information about the RStudio version, the OS and the R version? I tried your code and plot(mmr) is very fast. (RStudio 1.2.1090, Ubuntu 18.04, R 3.5.1 and Rstudio Server, MacOS 10.14.1, R 3.5.1)

@MichaelChirico
Copy link
Contributor Author

@pascaloettli added

@pascaloettli
Copy link

@MichaelChirico Thank you. I tried also in RStudio 1.2.1099 on MacOS 10.14.1 but, while a bit slower compared to Ubuntu, it is still fast. (For some unknown reason, it is very slow in R console running on iTerm2, ~40x slower.)

@ronblum
Copy link
Contributor

ronblum commented Dec 10, 2018

I am able to reproduce the problem with RStudio Desktop 1.2.1184-1 on MacOS 10.14.2 Mojave, nd when done, nothing is displayed. It works fine with RStudio Server 1.2.1186-1 on Ubuntu 16.04.5. On RStudio Desktop 1.1.463, the process never completed—it continued for at least 10 minute.

@MichaelChirico Can you clarify: on Ubuntu 18, were you using the desktop or server product? And for MacOS, did you mean desktop, or are you referring to using a Mac to connect to a remote server?

@ronblum ronblum added bug reproducible info needed Additional information requested—reprex, steps, open question, etc. labels Dec 10, 2018
@pascaloettli
Copy link

Strangely, I still cannot reproduce with RStudio Desktop 1.2.1184 on macOS Mojave 10.14.1. It takes about 2 seconds to plot and display the map.

@MichaelChirico
Copy link
Contributor Author

@ronblum I think only @pascaloettli was trying this on Ubuntu.

For me, I only ran this on my local machine (see <details> section of original post for all details)

@dmontecino
Copy link

Same here... MAC OS mojave
any solution?

@ronblum ronblum added qa repro and removed info needed Additional information requested—reprex, steps, open question, etc. labels Jan 2, 2020
@ronblum
Copy link
Contributor

ronblum commented Feb 7, 2020

This is still an issue in RStudio Desktop 1.3.799 on MacOS 10.15.3 (Catalina). We'll review the issue as we continue development of RStudio, but we unfortunately don't have a timeframe for a solution.

@ronblum ronblum removed the qa repro label Feb 7, 2020
@jakelawlor
Copy link

Hi, did anyone ever find an answer to this? I just got a new macbook pro (13 inch, 2020) and am having the same problem.

RstudioGD:

#user  system elapsed 
#128.162   0.510 129.271 

pdf:

# user  system elapsed 
# 0.423   0.027   0.460 

I am on rstudio version 1.3.1093 and mac big sur 11.1

@singhtanya88
Copy link

Hi, did anyone ever find an answer to this? I just got a new macbook pro (13 inch, 2020) and am having the same problem.

RstudioGD:

#user  system elapsed 
#128.162   0.510 129.271 

pdf:

# user  system elapsed 
# 0.423   0.027   0.460 

I am on rstudio version 1.3.1093 and mac big sur 11.1

I have exactly the same problem, but only on a Macbook Air 2019. I was hoping that an upgrade from Catalina to Mac Big Sur 11.1 would help. But it didn't. If you found the solution for this @jakelawlor I'd appreciate you sharing it. Thanks

@kevinushey
Copy link
Contributor

I've reproduced this locally as well. Here's a snippet of the call stack:

Screen Shot 2021-02-03 at 3 32 51 PM

For some reason, the calls are much more expensive through our shadow graphics device versus the "default" graphics device, but I'm not sure why.

@Polkas
Copy link

Polkas commented Mar 4, 2021

The same extremely slow plotting for me on my Mac Pro. Solution for others.

plot_2png <- function(x, filename) {
  x <- substitute(x)
  stopifnot(is.call(x))
  filename_path <- file.path(getwd(), filename)
  file.create(filename_path)
  png(filename_path)
  eval(x)
  dev.off()
}

usage for one or many plots.

plot_2png({
plot(pl)
plot(voi, add = TRUE)
}, "PATH/FILE.png")

Still not enough if sb want to use it in e.g. Rmd.

EDIT

I have a new observation that the same slow computation occurs from R base GUI.
Thus it looks to not be only a RStudio problem.
https://stat.ethz.ch/pipermail/r-sig-mac/2017-July/012446.html

@rosie-williams
Copy link

rosie-williams commented Mar 24, 2021

I'm having the same issue. Occasionally it will run as normal but most of the time Rstudio crashes and can't produce the plot. It definitely appears to be a graphics issue for me but I haven't worked out the differences yet between when it runs as normal or when it crashes Rstudio. However, I've found that if I run X11(type = "cairo") before the query then the performance time is normal in the graphics box.

@RaulMartinezRincon
Copy link

I had the same problem in a mac os. My solution was to install XQuartz (https://www.xquartz.org/), then use x11() to plot the spatial object, however you will need to set "usePolypath = FALSE". Example:

library(raster)
fraL1 <- getData('GADM', country='FRA', level=1) 
x11()
plot(fraL1, usePolypath = FALSE)

@pascaloettli
Copy link

Don't use the "(default)" Backend under General > Graphics.

With (default):

system.time(plot(mmr))
   user  system elapsed 
 79.262   0.248  79.713 

With Cairo:

system.time(plot(mmr))
   user  system elapsed 
  0.301   0.025   0.329 

With AGG:

system.time(plot(mmr))
   user  system elapsed 
  0.231   0.012   0.253 

RStudio 2021.09.1+372 "Ghost Orchid" Release (8b9ced1, 2021-11-08) for macOS
Mozilla/5.0 (Macintosh; Intel Mac OS X 12_1_0) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants