Having trouble getting my leaflet map to render in a 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. I have no idea if that would cause this issue though.

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)


#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) {

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)

     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")
     })
}
)

## Here is my data
datapasta::tribble_paste(melanoma)
tibble::tribble(
  ~stateFIPS,                 ~State,       ~Year, ~Value,
        "01",              "Alabama", "2012-2016",   21.4,
        "02",               "Alaska", "2012-2016",     26,
        "04",              "Arizona", "2012-2016",  21.89,
        "05",             "Arkansas", "2012-2016",  21.24,
        "06",           "California", "2012-2016",  22.47,
        "08",             "Colorado", "2012-2016",  22.13,
        "09",          "Connecticut", "2012-2016",  20.54,
        "10",             "Delaware", "2012-2016",  29.71,
        "11", "District of Columbia", "2012-2016",   9.48,
        "12",              "Florida", "2012-2016",   23.9,
        "13",              "Georgia", "2012-2016",  26.64,
        "15",               "Hawaii", "2012-2016",   21.4,
        "16",                "Idaho", "2012-2016",  27.91,
        "17",             "Illinois", "2012-2016",  19.62,
        "18",              "Indiana", "2012-2016",   20.1,
        "19",                 "Iowa", "2012-2016",  26.32,
        "20",               "Kansas", "2012-2016",  21.63,
        "21",             "Kentucky", "2012-2016",  26.87,
        "22",            "Louisiana", "2012-2016",  17.28,
        "23",                "Maine", "2012-2016",  25.94,
        "24",             "Maryland", "2012-2016",  23.01,
        "25",        "Massachusetts", "2012-2016",  21.15,
        "26",             "Michigan", "2012-2016",  19.49,
        "27",            "Minnesota", "2012-2016",  30.32,
        "28",          "Mississippi", "2012-2016",  17.14,
        "29",             "Missouri", "2012-2016",   19.1,
        "30",              "Montana", "2012-2016",  26.82,
        "31",             "Nebraska", "2012-2016",   23.9,
        "32",               "Nevada", "2012-2016",  14.99,
        "33",        "New Hampshire", "2012-2016",  30.17,
        "34",           "New Jersey", "2012-2016",  21.86,
        "35",           "New Mexico", "2012-2016",  15.33,
        "36",             "New York", "2012-2016",  18.06,
        "37",       "North Carolina", "2012-2016",  25.59,
        "38",         "North Dakota", "2012-2016",  23.26,
        "39",                 "Ohio", "2012-2016",  22.85,
        "40",             "Oklahoma", "2012-2016",  21.38,
        "41",               "Oregon", "2012-2016",  27.05,
        "42",         "Pennsylvania", "2012-2016",  24.27,
        "44",         "Rhode Island", "2012-2016",  22.47,
        "45",       "South Carolina", "2012-2016",  23.28,
        "46",         "South Dakota", "2012-2016",  24.13,
        "47",            "Tennessee", "2012-2016",  20.22,
        "48",                "Texas", "2012-2016",  12.74,
        "49",                 "Utah", "2012-2016",  39.76,
        "50",              "Vermont", "2012-2016",  35.76,
        "51",             "Virginia", "2012-2016",  19.21,
        "53",           "Washington", "2012-2016",  26.03,
        "54",        "West Virginia", "2012-2016",  20.88,
        "55",            "Wisconsin", "2012-2016",  23.91,
        "56",              "Wyoming", "2012-2016",  22.34
  )

I just get this blank space when I run the code.

Has anyone dealt with this issue before or know why my map is not rendering? Without an error message I am at a loss. Any help would be appreciated.

There seems to be nothing wrong with your code - although you may wish to consider moving from the 'old' {sp} package format to the 'new' {sf} package format by using US_states <- tigris::states(class = "sf") as the {sf} format is a modified data frame, so easier to debug & process via familiar {dplyr} syntax (ie. allowing dplyr::inner_join() instead of tigris::geo_join()).

Are you certain you are not running out of memory with your shiny app?

is the app on shinapps.io? if yes you may want to check the metrics and logs under your account settings

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

I'm not sure if I am running out of memory with my shiny app. This is my first attempt at creating a shiny dashboard and I am relatively new to R so I was not aware that could be a potential problem. How would I check that and if that was an issue is there a way for me to fix that?

I checked my memory. It doesn't appear that I am running out of memory. My instances are large and I am using 15KB. I did try using the sf package, but my leaflet map is still not rendering. I appreciate your help. I will keep troubleshooting.