3

I am working on an R Shiny project to visualize the spread of COVID19 around the world. When I run the app locally, it works just fine, but when I try to deploy the app, it runs into issues which I am assuming are related to storing the shapefiles: I get an error on the map page that reads, "An error has occurred. Check your logs or contact the app author for clarification."

Here is my code so far:

#Read in datasets
who_data <- read.csv("https://covid19.who.int/WHO-COVID-19-global-data.csv")
pops <- read.csv("https://gist.githubusercontent.com/curran/0ac4077c7fc6390f5dd33bf5c06cb5ff/raw/605c54080c7a93a417a3cea93fd52e7550e76500/UN_Population_2019.csv")


#download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", destfile="world_shape_file.zip")
#unzip("world_shape_file.zip")
#world_spdf=readOGR(dsn = getwd(),layer = "TM_WORLD_BORDERS_SIMPL-0.3")



#-----Preprocessing Data-----#

who_data$Date <- as.Date(who_data$Date_reported)

cols=colnames(pops)
pop_data=pops[,c(cols[1],cols[length(cols)])]
colnames(pop_data)=c("Country","Population")
pop_data$Population=pop_data$Population*1000

covid19_data=merge(x=who_data,y=pop_data,by="Country",all.x=TRUE)


covid19_data$Date_reported=NULL
covid19_data$Country_code=NULL
covid19_data <- covid19_data[order(covid19_data$Country,covid19_data$Date),]
covid19_data=covid19_data[c(1,2,7,8,3,4,5,6)]




#----- Load libraries -----#
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(rsconnect)
library(packrat)
library(formattable)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
library(RColorBrewer)
library(scales)
library(lattice)
library(dplyr)
library(reshape2)
library(plotly)



# Define UI for application 


ui <- fluidPage(
    dashboardPage(
        dashboardHeader(title="COVID19 Analysis"),
        dashboardSidebar(
            sidebarMenu(
                
                menuItem("Spread of the Virus",
                         tabName="map_spread",
                         icon=icon("viruses")
                ))
        ),
        dashboardBody(
            tabItems(
                
                tabItem(
                    tabName = "map_spread",
                    sliderInput("date_filter", "Date Filter",
                                min = min(covid19_data$Date), max = max(covid19_data$Date), value = min(covid19_data$Date)
                    ),
                    p("Not ready --> need to clean up data"),
                    leafletOutput("world_map")
                    
                )))))

# Define server logic 
server <- function(input, output) {
    
 
    #---------- WORLD MAP ----------#
    
    
    map_filter=reactive({
        filter=subset(covid19_data,Date==input$date_filter)
        return(filter)
    })
    
    
    merge_filter=reactive({
        names(world_spdf)[names(world_spdf) == "NAME"] <- "Country"
        map_data=merge(x=world_spdf,y=map_filter(),by="Country",all.x=TRUE)
    })
    
    
    
    
    #Choropleth Map 
    output$world_map=renderLeaflet({
        bins=c(0,100,500,1000,5000,10000,Inf)
        pal=colorBin(palette = "YlOrBr",domain = merge_filter()$Cumulative_cases,
                     na.color = "transparent",
                     bins=bins)
        customLabel = paste("Country: ",merge_filter()$Country,"<br/>",
                            "Cumulative cases: ",merge_filter()$Cumulative_cases, serp="") %>%
            lapply(htmltools::HTML)
        
        leaflet(merge_filter()) %>%
            addProviderTiles(providers$OpenStreetMap,options=tileOptions(minZoom = 2,
                                                                         maxZoom = 8)) %>%
            addPolygons(fillColor = ~pal(Cumulative_cases),
                        fillOpacity = 0.9,
                        stroke = TRUE,
                        color = "white",
                        highlight=highlightOptions(
                            weight=5,
                            fillOpacity = 0.3
                        ),
                        label=customLabel,
                        weight=0.3,
                        smoothFactor = 0.2) %>%
            
            addLegend(
                pal=pal,
                values = ~Cumulative_cases,
                position = "bottomright",
                title = "Cumulative cases"
            )
    })
    
    
    
}

# Run the application 
shinyApp(ui = ui, server = server)

Can someone help identify the proper way to store shape files when deploying an R Shiny app? I understand when deploying an R Shiny app using Excel files for data, I just store them in the same folder as the app.R file, but it seems these shape files are behaving in a weird way that I can't resolve. Any help would be appreciated! Thank you!

1 Answer 1

1

Files required by a published Shiny app should go in a folder named www. If you've got them in the same folder as app.R, they won't be detected properly. No idea why, honestly, but here's a couple resources that mention it:

https://shiny.rstudio.com/images/shiny-cheatsheet.pdf

https://subscription.packtpub.com/book/application_development/9781785280900/8/ch08lvl1sec53/the-www-directory

4
  • Thanks for the answer! I tried moving the shapefiles to the www folder and still find myself in the same situation. The www folder containing the shapfiles (.dbf file, .prj file, .shp file, .shx file, and .zip file) is in the same folder with the app.R file. Commented Oct 28, 2020 at 15:02
  • Interesting. What do your logs say? If you've deployed them on shinyapps.io, you should be able to see your app logs from the admin dashboard.
    – Dubukay
    Commented Oct 28, 2020 at 17:50
  • I have no messages in my R console, the only message I get is on the app stating, "An error has occurred. Check your logs or contact the app author for clarification." This error message is in place of the map. So that app loads, just the content (map) doesn't show up. This error usually is when something is saved locally, but not on the Shiny server. I know this has something to do with the shapefiles I am working with to create the map, but I don't know how to get them on the server. Commented Oct 28, 2020 at 23:51
  • 1
    Right, so what I'd recommend is debugging by placing print statements in your code that will be captured by the logs. If you're certain it's the shapefiles then perhaps more information isn't necessary, but I can imagine it'd help to know whether the files are being found and failing to load, or whether they're unable to be found at all, or some other error. So you could add output from things like list.files or even str(shapefile) to confirm that nothing weird is happening upon publication.
    – Dubukay
    Commented Oct 29, 2020 at 3:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.