Search code examples
rshinyr-leaflet

Trouble when splitting up Shiny Leaflet App


I'm trying to split my Shiny App to host on shinyapps.io, but when I split up the app, I lose the data.

When the ui and server are together, it works; but in separating them into two separate files, I'm losing the sidebar.

ui.R

library(shiny)
library(leaflet)
library(RColorBrewer)
library(shiny)

shinyUI(
  bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(uiOutput("sorted.data"), top = 10, right = 10
                )
    )
  )

and

server.r

library(shiny)
library(leaflet)
library(RColorBrewer)
library(maps)

capwords <- function(s, strict = FALSE) {
  cap <- function(s) paste(toupper(substring(s, 1, 1)),
                           {s <- substring(s, 2); if(strict) tolower(s) else s},
                           sep = "", collapse = " " )
  sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
}

# mapStates = map("state", fill = TRUE)
# data("us.cities")
map.cities(us.cities)
# leaflet(mapStates) %>% addTiles() %>% setView(lng = -97.00, lat = 38.00, zoom = 4)

# import and clean data
alumni <- read.csv("../Residency Match List 2010-2015 - Sheet1.csv", stringsAsFactors = F, header = T)
# convert city to lowercase then capitalize it, then paste it together with the state
# we're doing this to match the alumni data to the us.cities data
alumni$city.state <- paste(capwords(tolower(alumni$City)), alumni$State)

# merge the data by the city.state column we just created and the name column in the us.cities data
alumni.map.data <- merge(alumni, us.cities, by.x = "city.state", by.y = "name")

shinyServer(function(input, output, session) {
    
    # Reactive expression for the data subsetted to what the user selected
    filteredData <- reactive({
      alumni.map.data[alumni.map.data$Primary.Specialty == input$Primary.Specialty,]
    })
    
    output$sorted.data <- renderUI({sort(unique(alumni.map.data$Primary.Specialty))})
    
    output$map <- renderLeaflet({
      # Use leaflet() here, and only include aspects of the map that
      # won't need to change dynamically (at least, not unless the
      # entire map is being torn down and recreated).
      leaflet(alumni.map.data) %>% addTiles() %>%
        fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
    })
    
    # Incremental changes to the map (in this case, replacing the
    # circles when a new color is chosen) should be performed in
    # an observer. Each independent set of things that can change
    # should be managed in its own observer.
    observe({
      
      leafletProxy("map", data = filteredData()) %>%
        clearMarkers() %>% clearMarkerClusters() %>%
        addCircleMarkers(
          popup = ~as.character(paste(sep = "<br/>",
                                      paste0('<a href = ', #begin link format to search google
                                             "https://www.google.com/#q=",
                                             paste(First.Name,
                                                   Last.Name, "Tulane", sep = "+"), #search elements
                                             ">",
                                             paste(First.Name, Last.Name, sep = " "),
                                             '</a>'),  # end link format to search google
                                      paste("Class of", Class.Year, sep = " "),Program, 
                                      Primary.Specialty)),
          clusterOptions = markerClusterOptions()
        )
    })
  })

When I run this code the map is rendered, but without any of the clusters/markers and I get the following error:

Warning in charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
all but the first element will be ignored
Assuming 'long' and 'lat' are longitude and latitude, respectively

I feel like I'm missing something simple.


Solution

  • I'm not 100% on this because I can't recreate so I'm guessing, but I would take out everything that's in your server.r file before the

    shinyServer(function(input, output, session){
    

    line and put it into a global.r file in the same directory.

    Same applies for all the library calls, just have them all in global.r