Tj Pierce
Tj Pierce

Reputation: 25

My leaflet map will not render in my shiny dashboard

I've been trying to render a leaflet map I created in a shiny dashboard tab box. The leaflet code works fine in a regular script and the map renders in the viewer. However, when I add my code to the ui.r and server.r all I get is a blank space in the dashboard where the map should be. I'm not receiving any errors so I'm stumped as far as figuring out what could be wrong. I've read that shiny sometimes has problems when the height and width of the map are specified, but I left this default so I do not think that is the problem. The only other thing I can think of is that some of the objects from the packages I am using are being masked and some of my packages are not up to date. I have no idea if that would cause this issue though.

My data: enter image description here

Here is my code:

library(shiny)
library(shinydashboard)
library(leaflet)
library(tigris)
library(dplyr)
library(tidyverse)
library(leaflet.extras)
library(rmapshaper)
library(rsconnect)
library(plotly)
library(ggplot2)
library(rgdal)
library(htmltools)
library(shinyWidgets)
library(DT)

melanoma_incidence <- read_excel("Incidence_data.xlsx")
US_states <- tigris::states()
US_Join <- geo_join(US_States, Melanoma_Incidence, by_sp = "GEOID", by_df = "stateFIPS")
US_Join <- subset(US_Join, !is.na(Value))

pal <- colorNumeric("Reds", domain = US_Join$Value)
labels <- sprintf(
  "<strong>%s</strong><br/>%g",
  US_Join$State, US_Join$Value) %>% lapply(htmltools::HTML)

ui.r

shinyUI(
    dashboardPage(
        dashboardHeader(title = "UV and Skin Cancer"),
     dashboardBody(
            tabItems(
                tabItem(tabName = "Homepage", h2("UV Exposure and Melanoma"),
                tabItem(tabName = "Homepage",
                fluidRow(
                tabBox(id="National Map",
                tabPanel("Melanoma Incidence", leafletOutput("mymap"))
                                ))))))))
server.r

shinyServer(function(input, output) {
     output$mymap <- renderLeaflet({
       leaflet() %>% addProviderTiles(providers$CartoDB.Positron) %>%
         setView(-96, 37.8, 4) %>%
         addPolygons(data = US_Join,
                     fillColor = ~pal(US_Join$Value),
                     fillOpacity = 0.9,
                     weight = 0.2,
                     smoothFactor = 0.2,
                     label = labels,
                     labelOptions = labelOptions(
                       style = list("font-weight" = "normal", padding = "3px 8px"),
                       textsize = "15px",
                       direction = "auto")) %>%
         addLegend(pal = pal,
                   values = US_Join$Value,
                   position = "bottomright",
                   title = "Incidence")
     })
}
)

The code runs, but I just get a blank space in my shiny dashboard. I've posted a picture below. enter image description here

Anyone have an idea as to why my map isn't rendering? Without an error message I am at a loss. EDIT: I added in the rest of my code and my data.

Upvotes: 1

Views: 1144

Answers (1)

user12728748
user12728748

Reputation: 8506

Without a reproducible example it is hard to know. You might be missing some global variables required in ui.R or server.R, you may have empty values, or something else may be going on.

The code below works in a shiny app, at least - if that helps you troubleshooting:

library(leaflet)
library(raster)
#> Loading required package: sp
library(shiny)
US_Join <- getData('GADM', country='US', level=1)
set.seed(1)
US_Join$Value <- sample(1:100, 51, replace=TRUE)
pal <- colorNumeric(
  palette = "Blues",
  domain = US_Join$Value)
labels <- sprintf(
  "<strong>%s</strong><br/> <strong>Incidence:</strong> %g",
  US_Join$NAME_1, US_Join$Value
) %>% lapply(htmltools::HTML)


server <- shinyServer(function(input, output) {
  output$mymap <- renderLeaflet({
    leaflet() %>% addProviderTiles(providers$CartoDB.Positron) %>%
      setView(-96, 37.8, 4) %>%
      addPolygons(data = US_Join,
                  fillColor = ~pal(US_Join$Value),
                  fillOpacity = 0.9,
                  weight = 0.2,
                  smoothFactor = 0.2,
                  label = labels,
                  labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto")) %>%
      addLegend(pal = pal,
                values = US_Join$Value,
                position = "bottomright",
                title = "Incidence")
  })
}
)

ui <- tabPanel("Melanoma Incidence", leafletOutput("mymap"))

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions