Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up leaflet on a shiny server

Tags:

r

leaflet

shiny

I have set up a simply leaflet map in shiny where the shiny server.R looks like this:

(please get the RDS-Data from Dropbox for a reproducible example)

Server.R

test_polygons <- readRDS('test_polygons.RDS') # Sind die Polygon-Shapefiles transformiert auf WGS84 für Bezirke

#some merging....
#we use sample data instead

test_polygons@data$sample <- runif(nrow(test_polygons@data))

#Create some nice popups
world_popup <- function(modell){
  niveau <- test_polygons@data[, modell]
  
  probs  <- seq(0, 1, length.out = 5 +  1)
  niveau <- cut(niveau, breaks=quantile(niveau, probs, na.rm = TRUE, names = FALSE), labels=paste('level', 0:4), include.lowest = TRUE)
  niveau <- as.character(niveau)
  
  
  niveau <- factor(niveau, labels=)
  
  paste0("<strong>Bezirk: </strong>", 
         as.character(test_polygons@data$ID), 
         "<br><strong><br>", 
         "</strong>", 
         "<strong>Level: </strong>", 
         niveau
  )
}


  tiles <- "http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
  attribution <- 'Map tiles by <a target="_blank" href="http://stamen.com">Stamen Design</a>, under <a target="_blank" href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Map data by <a target="_blank" href="http://www.naturalearthdata.com/">Natural Earth</a>.'



# produce the leaflet map ====
pal <-  colorQuantile("YlOrRd", NULL, n = 5)
      m.sample <- leaflet(data = test_polygons) %>%
      addTiles(urlTemplate = tiles,  
      attribution = attribution) %>%
      setView(13.782778,  47.61, zoom = 7) %>%
      addPolygons(fillColor = ~pal(test_polygons$sample), 
      fillOpacity = 0.8, 
      color = "#000000", 
      weight = 1, 
      popup = world_popup('sample'))
      
      # start the server part
      server <- function(input, output, session) {
        output$query <- renderText({
          as.character(parseQueryString(session$clientData$url_search))
        })
        
        output$mymap <- renderLeaflet({   
              m.sample
        })
      }      

ui.R

Whilst the UI is fairly simple:

require(leaflet)
require(shiny)

ui <- fluidPage(
  column(width=12,
  leafletOutput("mymap", height="200px")#, height="700px")
  )
)

This works okay on my desktop computer. However, as soon as I try to access it on my server the leaflet-map loads awfully slowly. Especially if I alter the height to, say, 100 % it stops loading at all. So here are my questions:

  • How do I speed up the loading process.
  • Is it possible to load some parts in advance since anything is reactive in this context.
  • Can I create a map that is independent from shiny -- probably this one's loading faster.
  • Is it possible that my polygons have to many details?

Thank you very much for your help!

like image 875
Seb Avatar asked Mar 30 '16 19:03

Seb


Video Answer


1 Answers

Starting from the comments simplifying the SP-Object did the trick. I imported the underlying shapefile in QGis and adjusted it by

 Vector => Geometry Tools => Simplify geometries

Works much faster now. More info can be found via:

Qgis-Stackexchange or the Documentation.

Thanks for your help!

like image 100
Seb Avatar answered Oct 17 '22 16:10

Seb