Created
March 16, 2019 15:27
-
-
Save rpodcast/1a7552d5c6269d2fe488ef072ce64cfb to your computer and use it in GitHub Desktop.
POC of rayshader in Shiny
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Must be executed BEFORE rgl is loaded on headless devices. | |
options(rgl.useNULL=TRUE) | |
library(shiny) | |
library(rgl) | |
library(rayshader) | |
library(magrittr) | |
#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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xPts <- runif(1000, 0, 1) | |
yPts <- runif(1000, 0, 1) | |
zPts <- runif(1000, 0, 1) | |
shinyServer(function(input, output, session) { | |
# Expression that generates a rgl scene with a number of points corresponding | |
# to the value currently set in the slider. | |
output$sctPlot <- renderRglwidget({ | |
try(rgl.close()) | |
ambmat = ambient_shade(elmat) | |
elmat_3d <- 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() | |
rglwidget() | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Shiny WebGL!"), | |
# Sidebar with a slider input for number of points | |
sidebarPanel( | |
sliderInput("pts", | |
"Number of points:", | |
min = 10, | |
max = 1000, | |
value = 250) | |
), | |
# Show the generated 3d scatterplot | |
mainPanel( | |
rglwidgetOutput("sctPlot") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @rpodcast,
I'm wondering if you can share any tips on rendering rayshader objects in Shiny apps more efficiently?
I'm trying to get a couple of maps of Hawaii, so I've adapted the method for {
geoviz
} and {rayshader
} to match your example a bit closer.However, I still think these objects are much larger than your example above, and when I've tried pushing to shinyapps.io with
rsconnect()
it's crashing on free tier due to memory.Any tips you could share would be greatly appreciated!
sessionInfo()
: