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)