Search code examples
rshinyleafletr-leafletshinyapps

r leaflet shiny app in shiny.io error


I'm trying to run my shiny app on shinyapp.io.

https://mrmoleje.shinyapps.io/north-america-massacres/

The app runs fine in R Studio, however in the server the 'popups' in my leaflet map completely disappear. There isn't anything in the shiny.io log to help me, and I can't find any guidance online. Below is the code for the app:

d <- data.frame(massacre_name = c("name1", "name2"),
            date = c(1345, 6754),
            native_casualties=c(0, 0),
            Tribe_name=c("named", "named"),
            latitude=c(30.2, 32.4),
            longitude=c(-84, -87.1),
            web=c("www.address.com", "www.address2.com")
            )

#load libraries----
library(readxl)
library(leaflet)
library(dplyr)
library(htmltools)
library(shiny)
library(shinythemes)

#create the UI
ui <- {fluidPage(theme = shinytheme("slate"), titlePanel("Massacres in 
North America involving 
                                                     First Nations Peoples: 1500-1700"), 
             sidebarLayout(position = "right",
                           sidebarPanel(
                             selectInput(inputId = "input1", label = "Tribe name" ,choices = 
                                           unique(d$Tribe_name))

                           ),

                           mainPanel(
                             leafletOutput("mymap"))
             )
)}


server <- function(input, output) {
 react <- reactive({
req(input$input1)
df <- d[d$Tribe_name == input$input1,]
df
  }) 

 output$mymap <- renderLeaflet({ req(input$input1)

leaflet(data = react()) %>% addTiles() %>% setView(lng = -100.94, lat = 38.94 , zoom = 3.5) %>% 
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
  addMarkers(lng = ~longitude, lat= ~latitude, 
             popup = paste(react()$massacre_name, "<br>", "Date:", 
react()$date, 
                           "<br>", "Number of native casualties:", 
react()$native_casualties,
                           "<b><a href"= react()$web))

  })
}


shinyApp(ui, server)

Any ideas of why the popups don't appear in the server version?


Solution

  • I think the problem is that you dont define an icon for the addMarkers. If you change that function to addCircleMarkers your app works, also with the popups.

    And if you create an icon and include that in the addMarkers it should work too. It does for me. :)

    #load libraries----
    library(leaflet)
    library(dplyr)
    library(htmltools)
    library(shiny)
    library(shinythemes)
    
    d <- data.frame(massacre_name = c("name1", "name2"),
                    date = c(1345, 6754),
                    native_casualties=c(0, 0),
                    Tribe_name=c("named1", "named2"),
                    latitude=c(30.2, 32.4),
                    longitude=c(-84, -87.1),
                    web=c("www.address.com", "www.address2.com"), stringsAsFactors = F
    )
    
    #create the UI
    ui <- {fluidPage(theme = shinytheme("slate"), titlePanel("Massacres in 
                                                             North America involving 
                                                             First Nations Peoples: 1500-1700"), 
                     sidebarLayout(position = "right",
                                   sidebarPanel(
                                     selectInput(inputId = "input1", label = "Tribe name" ,choices = 
                                                   unique(d$Tribe_name))
    
                                   ),
    
                                   mainPanel(
                                     leafletOutput("mymap")
                                     )
                     )
    )}
    
    
    server <- function(input, output) {
    
    
      react <- reactive({
        req(input$input1)
        df <- d[d$Tribe_name == input$input1,]
        df
      }) 
    
      greenLeafIcon <- makeIcon(
        iconUrl = "http://leafletjs.com/examples/custom-icons/leaf-green.png",
        iconWidth = 38, iconHeight = 95,
        iconAnchorX = 22, iconAnchorY = 94,
        shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
        shadowWidth = 50, shadowHeight = 64,
        shadowAnchorX = 4, shadowAnchorY = 62
      )
    
      output$mymap <- renderLeaflet({ req(input$input1)
    
        leaflet(data = react()) %>% addTiles() %>% setView(lng = -100.94, lat = 38.94 , zoom = 3.5) %>% 
          addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
          addMarkers(lng = react()$longitude, lat= react()$latitude, icon=greenLeafIcon, 
                     popup = paste(react()$massacre_name, "<br>", "Date:",
                                   react()$date,
                                   "<br>", "Number of native casualties:",
                                   react()$native_casualties,
                                   "<b><a href"= react()$web)
          ) 
      })
    }
    
    shinyApp(ui, server)