tmap/ leaflet not updating with selectInput - disconnecting from server

Hi, I'm trying to create an interactive map in shiny where the user can select a year and the map updates accordingly. It works fine when I run it locally. In shiny, the default map works and is interactive, but when I change the input I get a "disconnected from the server" message. The logs show that this is a memory problem, but I can't figure out how to fix this.

Here is an example to give an idea of what I'm trying to do. In my actual map I have created the tmap objects in a separate file .R. Ideally I'm aiming to have separate maps for years 2000-2014

# relevant packages
library(shiny)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(tmap)
library(leaflet)
library(spData)
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`

# Example data set:

example <- tibble(Name = c("Northland",
                           "Auckland",
                           "Waikato",
                           "Bay of Plenty",
                           "Gisborne",
                           "Hawke's Bay",
                           "Taranaki",
                           "Manawatu-Wanganui",
                           "Wellington",
                           "West Coast",
                           "Canterbury",
                           "Otago",
                           "Southland",
                           "Tasman",
                           "Nelson",
                           "Marlborough"), year1 = c(1:16), year2 = c(17:32))

# Joining the example data set to nz, an object that has geospatial information for the map.

# This is not the map I'm actually using, but used this one in order to make this reproducible example. 

joined_data <- inner_join(nz, example, by = "Name")

# A function to create the tmap objects. Ideally I'd want to have 15 in my actual app. 

make_map <- function(y, z) {
  show_map <- tm_shape(joined_data) +
    tm_polygons(col = y, palette = "viridis", title = z)
  
  tmap_leaflet(show_map)
}

# First map object. 

map1 <- make_map("year1", "Total") 

# Second map object.     
     
map2 <- make_map("year2", "Total") 


ui <- mainPanel("Problem", fluidRow(column(7, leafletOutput(outputId = "map")),
                               selectInput(inputId = "choose", choices = c("year1", "year2"), 
                                           label = "Choose")))
                               
  
  server <- function(input, output) {
    
# Have tried using both renderLeaflet and renderTmap, but neither work.  

    output$map <- renderLeaflet({
      
        
        if(input$choose == "year1") {
          map1
          
        }
    
        else if(input$choose == "year2") {
          map2
          
          
        }
      
      
    })
    
    
  }

# Run the application 
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

Shiny applications not supported in static R Markdown documents

Created on 2020-12-10 by the reprex package (v0.3.0)

Your code works for me, running in a browser window. Did you remember to upload your tmap files along with the app? Another thing you can do is have your app write messages to the console during execution, then you can check the console log to see what happened.

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252    LC_MONETARY=English_New Zealand.1252
[4] LC_NUMERIC=C                         LC_TIME=English_New Zealand.1252    

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

other attached packages:
 [1] spData_0.3.8    tmap_3.2        sf_0.9-6        forcats_0.5.0   stringr_1.4.0   dplyr_1.0.2     purrr_0.3.4     readr_1.4.0    
 [9] tidyr_1.1.2     tibble_3.0.4    ggplot2_3.3.2   tidyverse_1.3.0 shiny_1.5.0     leaflet_2.0.3  

loaded via a namespace (and not attached):
 [1] httr_1.4.2              viridisLite_0.3.0       jsonlite_1.7.1          modelr_0.1.8            assertthat_0.2.1       
 [6] sp_1.4-4                cellranger_1.1.0        yaml_2.2.1              lattice_0.20-41         pillar_1.4.6           
[11] backports_1.2.0         glue_1.4.2              digest_0.6.27           RColorBrewer_1.1-2      promises_1.1.1         
[16] rvest_0.3.6             leaflet.providers_1.9.0 colorspace_1.4-1        htmltools_0.5.0         httpuv_1.5.4           
[21] XML_3.99-0.3            pkgconfig_2.0.3         raster_3.3-13           broom_0.7.2             haven_2.3.1            
[26] stars_0.4-3             xtable_1.8-4            scales_1.1.1            later_1.1.0.1           generics_0.1.0         
[31] ellipsis_0.3.1          withr_2.3.0             leafsync_0.1.0          cli_2.1.0               magrittr_1.5           
[36] crayon_1.3.4            readxl_1.3.1            mime_0.9                fs_1.5.0                fansi_0.4.1            
[41] xml2_1.3.2              lwgeom_0.2-5            class_7.3-17            tools_3.6.3             hms_0.5.3              
[46] lifecycle_0.2.0         munsell_0.5.0           reprex_0.3.0            compiler_3.6.3          e1071_1.7-4            
[51] tinytex_0.27            rlang_0.4.8             classInt_0.4-3          units_0.6-7             grid_3.6.3             
[56] tmaptools_3.1           dichromat_2.0-0         rstudioapi_0.11         htmlwidgets_1.5.2       crosstalk_1.1.0.1      
[61] base64enc_0.1-3         leafem_0.1.3            codetools_0.2-16        gtable_0.3.0            abind_1.4-5            
[66] DBI_1.1.0               R6_2.5.0                lubridate_1.7.9         fastmap_1.0.1           KernSmooth_2.23-18     
[71] stringi_1.5.3           parallel_3.6.3          Rcpp_1.0.5              png_0.1-7               vctrs_0.3.4            
[76] dbplyr_2.0.0            tidyselect_1.1.0        xfun_0.19

I've recently encountered a similar issue with a tmap app that runs fine on RStudio Server but fails on Shiny Server with the message "Disconnected from the server.". After a lot of troubleshooting I believe the issue is caused by a limit to the number of shape objects that can be rendered in Shiny.

I've searched the documentation and forums but can't find anything to confirm this limitation or whether there is an environment variable that can be increased somewhere.

If anyone has any further information regarding this issue, it would be a great help.

your code example runs fine on shinyapps.io
https://nguk.shinyapps.io/testtmapdeploy/
(I will probably delete this after a couple of days)
so perhaps you have an issue with your shiny server, is there a sys admin you can talk to ?

I suppose you should determine the memory availability of your server (what is there for you),
and the memory requirements of your app. If one doesnt fit in the other, get more resource, or think of ways to reduce your memory useage.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.