tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.05k stars 211 forks source link

plot_3d() not working #43

Closed tomauer closed 5 years ago

tomauer commented 5 years ago

I'm having trouble getting plot_3d to work correctly on Mac Mojave. I'm using this example from the demo.

options(rgl.useNULL = FALSE)

library(rayshader)

#Here, I load a map with the raster package.
loadzip = tempfile() 
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)

#And convert it to a matrix:
elmat = matrix(raster::extract(localtif,raster::extent(localtif),buffer=1000),
               nrow=ncol(localtif),ncol=nrow(localtif))

ambmat = ambient_shade(elmat)

elmat %>%
  sphere_shade(texture = "desert") %>%
  add_water(detect_water(elmat), color="desert") %>%
  add_shadow(ray_shade(elmat,zscale=3,maxsearch = 300),0.5) %>%
  add_shadow(ambmat,0.5) %>%
  plot_3d(elmat,zscale=10,fov=0,theta=135,zoom=0.75,phi=45, windowsize = c(1000,800))
render_snapshot()

It produces a black square, either in RStudio or R opened from command line and if I try to save it instead of rendering it. My sessionInfo():

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.4

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.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] rayshader_0.10.2

loaded via a namespace (and not attached):
 [1] progress_1.2.0          xfun_0.6                remotes_2.0.2          
 [4] purrr_0.3.2             lattice_0.20-38         colorspace_1.4-1       
 [7] testthat_2.0.1          miniUI_0.1.1.1          usethis_1.4.0          
[10] htmltools_0.3.6         rlang_0.3.4             pkgbuild_1.0.2         
[13] manipulateWidget_0.10.0 later_0.8.0             glue_1.3.1             
[16] withr_2.1.2             sp_1.3-1                sessioninfo_1.1.1      
[19] jpeg_0.1-8              foreach_1.4.4           plyr_1.8.4             
[22] stringr_1.4.0           munsell_0.5.0           raster_2.8-19          
[25] htmlwidgets_1.3         devtools_2.0.1          codetools_0.2-15       
[28] memoise_1.1.0           knitr_1.22              callr_3.2.0            
[31] doParallel_1.0.14       httpuv_1.5.1            ps_1.3.0               
[34] crosstalk_1.0.0         parallel_3.5.2          curl_3.3               
[37] Rcpp_1.0.1              xtable_1.8-4            scales_1.0.0           
[40] backports_1.1.3         promises_1.0.1          desc_1.2.0             
[43] pkgload_1.0.2           webshot_0.5.1           jsonlite_1.6           
[46] mime_0.6                fs_1.2.6                hms_0.4.2              
[49] bmp_0.3                 png_0.1-7               digest_0.6.18          
[52] stringi_1.4.3           tiff_0.1-5              processx_3.3.0         
[55] shiny_1.3.2             grid_3.5.2              rprojroot_1.3-2        
[58] rgdal_1.3-6             cli_1.1.0               tools_3.5.2            
[61] magrittr_1.5            rgl_0.100.19            crayon_1.3.4           
[64] readbitmap_0.1.5        pkgconfig_2.0.2         prettyunits_1.0.2      
[67] imager_0.41.2           assertthat_0.2.1        iterators_1.0.10       
[70] R6_2.4.0                igraph_1.2.4.1          compiler_3.5.2 

I thought I might have been having problems with rgl, so I made sure these examples worked (https://cran.r-project.org/web/packages/rgl/vignettes/rgl.html), which did after I installed rgl with XQuartz open.

Any help is appreciated.

tylermorganwall commented 5 years ago

See https://github.com/tylermorganwall/rayshader/issues/32 to see if that fixes the issue for you.

tomauer commented 5 years ago

Unfortunately, that doesn't fix it for me.

tomauer commented 5 years ago

I can use writeWebGL() successfully with and XQuartz open:

options(rgl.useNULL = FALSE)

library(rayshader)
library(rgl)

#Here, I load a map with the raster package.
loadzip = tempfile() 
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)

#And convert it to a matrix:
elmat = matrix(raster::extract(localtif,raster::extent(localtif),buffer=1000),
               nrow=ncol(localtif),ncol=nrow(localtif))

ambmat = ambient_shade(elmat)

elmat %>%
  sphere_shade(texture = "desert") %>%
  add_water(detect_water(elmat), color="desert") %>%
  add_shadow(ray_shade(elmat,zscale=3,maxsearch = 300),0.5) %>%
  add_shadow(ambmat,0.5) %>%
  plot_3d(elmat,zscale=10,fov=0,theta=135,zoom=0.75,phi=45, background = 'white')

writeWebGL(dir = "~/Downloads/test")
tylermorganwall commented 5 years ago

Are you using RStudio Server or RStudio Desktop?

tomauer commented 5 years ago

RStudio Desktop.

I was finally able to get it to work, but I have to manually open XQuartz application before I open RStudio, otherwise, it gives me a black screen.

tylermorganwall commented 5 years ago

Sounds like there just might be an issue with your R installation (I recommend reinstalling R and see if that helps). I develop on Mojave and haven't had any issues, so it's likely a local installation problem. Feel free to comment on this thread if you have any more related issues.

loomalaine commented 4 years ago

For the record, I'm on Catalina, and I had this same problem. Opening XQuartz before RStudio worked.

sauptrain commented 4 years ago

I am on Catalina 10.15.6. I have the same problem. I don't have RStudio but am using R Console.

I get below error message upon loading rayshader library and black page upon rendering the plot using plot_3d().

options(rgl.useNULL = FALSE)
library(ggplot2)
library(whitebox)
library(raster)
library(spatstat)
library(spatstat.utils)
library(suncalc)
library(sp)
library(lubridate)
library(rgdal)
> library(rayshader)
Warning messages:
1: In rgl.init(initValue, onlyNULL) : RGL: unable to open X11 display
2: 'rgl.init' failed, running with 'rgl.useNULL = TRUE'.
> library(rayrender)