shiny with swemaps2 and simplevis

Viewed 24

I am trying to make a simple interactive map with the package swemaps2 but i am having trouble getting it to work and i don't know why. This is my code so far and the data county is in the package swemaps2:

library(shiny)
library(shinydashboard)
library(swemaps2)
library(simplevis)
library(leaflet)
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(title = "Map dashboard test"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Map", tabName = "map", icon = icon("globe")),
      menuItem("Widgets", tabName = "widgets")
    )
  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "map",
              #tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
              leafletOutput("mymap")
      ),
      
      # Second tab content
      tabItem(tabName = "widgets",
              h2("Widgets tab content")
      )
    )
  )
)


server <- function(input, output) { 
  
  output$mymap = renderLeaflet(
    
    county %>% 
      mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10)) %>% 
      leaf_sf_col(col_var = random_number)   
    )  
}

shinyApp(ui, server)

The code bit:

    county %>% 
      mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10)) %>% 
      leaf_sf_col(col_var = random_number)

works fine outside the shiny server. My current guess is that it has something to do with the interactive part of the map but i have hit a wall in knowing where to look next. Any help would be appreciated

1 Answers

Hello from the creator of swemaps2, glad you found the package. I'm not exactly sure why simplevis does not work in Shiny.

However, we can use a package like mapview instead to get similar functionality. The trick for that package is to create the map outside renderLeaflet() and then use it in the function:

server <- function(input, output) {
  
  m <- county %>% 
    mutate(random_number = round(rnorm(nrow(.), mean = 100, sd = 10))) %>% 
    mapview::mapview(zcol = "random_number")
  
  output$mymap <- renderLeaflet({
    m@map
  })
}

Hope this is enough, I have written an issue on simplevis Github and might change my documentation to mapview instead.

Related