3

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?

SeGa
  • 9,454
  • 3
  • 31
  • 70
Mrmoleje
  • 453
  • 1
  • 12
  • 35
  • 1
    Can you include some dummy data or a link to the excel-sheet? – SeGa Jun 23 '18 at 22:35
  • @SeGa dummy data now added, thanks – Mrmoleje Jun 24 '18 at 07:55
  • [This link](https://shiny.rstudio.com/articles/sanitize-errors.html) might help you figure out the problem. Are all libraries installed on the server? – MLavoie Jun 24 '18 at 11:50
  • Okay thanks I’ll have a look. I omitted the idea that it might be a library issue, as ‘popup’ (the part of the map that isn’t working) is part of the leaflet library. If leaflet wasn’t installed on the server then the map wouldn’t work at all – Mrmoleje Jun 24 '18 at 11:57
  • @MLavoie just out of interest though, how would I check which libraries are installed on the server? – Mrmoleje Jun 24 '18 at 11:59
  • In my experiance, this is probably due to an error in the popups. Try commenting out that line, and see if the markers appear. – Wimpel Jun 24 '18 at 11:59
  • @Wimpel hmm interesting, the markers don't appear at all when I take out the popup (same result as original question - the map shows but no markers or popups) – Mrmoleje Jun 24 '18 at 13:13
  • I've added the shiny.io web address to give a bit more context – Mrmoleje Jun 24 '18 at 13:17
  • @Mrmoleje, the libraries you call in your app with `library(*)`, will be installed on shinyapps.io. Do you load in the data with `read_xlsx()`? Is that data showing up if you include it in table or print output? – SeGa Jun 24 '18 at 14:07

1 Answers1

3

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)
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • Ah how interesting! I wonder why AddMarker isn't recognised but add circle marker was. I might just keep the green leaf :) thanks for your help. – Mrmoleje Jun 24 '18 at 15:10