rshinyshinyappsr-leaflet

Error in displaying maps when hosting flexdashboard on shinyapps.io


I have created a flexdashboard with interactive shiny components, including a map.

---
title: "Coronavirus Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    theme: readable
runtime: shiny
---

```{r setup, include=FALSE, echo=FALSE, warning=FALSE}
library(flexdashboard)
library(leaflet)
library(rgdal)
library(readr)
library(dplyr)
library(sf)
library(ggplot2)
library(tmap)

The map displays data sourced from an online .csv and is created using a shape file sourced from a .zip file downloaded and stored locally in a directory named data:

renderLeaflet({
covid_cases <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")

# .zip downloaded from: https://www.naturalearthdata.com/downloads/50m-cultural-vectors/50m-admin-0-countries-2/
countries <- st_read("data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp", stringsAsFactors=FALSE, quiet=TRUE)

After some processing of the data to create a single dataframe combining the covid_cases and countries dataframes, the app plots cases per country:

map <- tm_shape(map_and_cases) + tm_polygons("Percent of Population", id="country_name")
tmap_leaflet(map)
})

This works wonderfully in RStudio whenever I run the document. However, whenever I attempt to host the dashboard on shinyapps.io, I encounter the following error message:

"Error: An error has occurred. Check your logs or contact the app author for clarification."

Checking the logs I have that:

2020-03-24T14:41:08.709221+00:00 shinyapps[1985429]: List of 3
2020-03-24T14:41:08.709701+00:00 shinyapps[1985429]:  $ echo   : logi FALSE
2020-03-24T14:41:08.862558+00:00 shinyapps[1985429]: 
  |                                                                       
  |.........................................                        |  64%
2020-03-24T14:41:08.710474+00:00 shinyapps[1985429]:  $ message: logi FALSE
2020-03-24T14:41:08.710489+00:00 shinyapps[1985429]: 
2020-03-24T14:41:08.862596+00:00 shinyapps[1985429]:   ordinary text without R code
2020-03-24T14:41:08.862597+00:00 shinyapps[1985429]: 
2020-03-24T14:41:08.862698+00:00 shinyapps[1985429]: 
  |                                                                       
  |...............................................                  |  73%
2020-03-24T14:41:08.863090+00:00 shinyapps[1985429]: label: unnamed-chunk-3
2020-03-24T14:41:08.876049+00:00 shinyapps[1985429]: 
  |                                                                       
  |.....................................................            |  82%
2020-03-24T14:41:08.876064+00:00 shinyapps[1985429]:   ordinary text without R code
2020-03-24T14:41:08.876064+00:00 shinyapps[1985429]: 
2020-03-24T14:41:08.876168+00:00 shinyapps[1985429]: 
  |                                                                       
  |...........................................................      |  91%
2020-03-24T14:41:08.876547+00:00 shinyapps[1985429]: label: unnamed-chunk-4
2020-03-24T14:41:08.888545+00:00 shinyapps[1985429]:   ordinary text without R code
2020-03-24T14:41:08.889867+00:00 shinyapps[1985429]: output file: /tmp/RtmpJQ4ULR/covid_dashboard.knit.md
2020-03-24T14:41:08.889868+00:00 shinyapps[1985429]: 
2020-03-24T14:41:08.888693+00:00 shinyapps[1985429]: 
2020-03-24T14:41:08.899598+00:00 shinyapps[1985429]: /opt/connect/ext/pandoc2/pandoc +RTS -K512m -RTS /tmp/RtmpJQ4ULR/covid_dashboard.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output /tmp/RtmpJQ4ULR/file1a850267e7e.html --email-obfuscation none --standalone --section-divs --template /opt/R/3.6.1/lib/R/library/flexdashboard/rmarkdown/templates/flex_dashboard/resources/default.html --id-prefix section- --variable 'theme:readable' --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --lua-filter /opt/R/3.6.1/lib/R/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /opt/R/3.6.1/lib/R/library/rmarkdown/rmd/lua/latex-div.lua --include-in-header /tmp/RtmpJQ4ULR/file1a873f90aahtml --highlight-style pygments --include-before-body /tmp/RtmpJQ4ULR/file1a831797393.html --include-after-body /tmp/RtmpJQ4ULR/file1a857f3714c.html 
2020-03-24T14:41:09.009122+00:00 shinyapps[1985429]: 
2020-03-24T14:41:09.009124+00:00 shinyapps[1985429]: Output created: /tmp/RtmpJQ4ULR/file1a850267e7e.html
2020-03-24T14:41:08.888512+00:00 shinyapps[1985429]: 
  |                                                                       
  |.................................................................| 100%
2020-03-24T14:41:08.888546+00:00 shinyapps[1985429]: 
2020-03-24T14:41:09.758017+00:00 shinyapps[1985429]: Parsed with column specification:
2020-03-24T14:41:09.758019+00:00 shinyapps[1985429]: cols(
2020-03-24T14:41:09.758022+00:00 shinyapps[1985429]: )
2020-03-24T14:41:09.758021+00:00 shinyapps[1985429]:   .default = col_double(),
2020-03-24T14:41:09.758021+00:00 shinyapps[1985429]:   `Province/State` = col_character(),
2020-03-24T14:41:09.786949+00:00 shinyapps[1985429]:   `Province/State` = col_character(),
2020-03-24T14:41:09.786947+00:00 shinyapps[1985429]: Parsed with column specification:
2020-03-24T14:41:09.758021+00:00 shinyapps[1985429]:   `Country/Region` = col_character()
2020-03-24T14:41:09.786948+00:00 shinyapps[1985429]: cols(
2020-03-24T14:41:09.758672+00:00 shinyapps[1985429]: See spec(...) for full column specifications.
2020-03-24T14:41:09.786949+00:00 shinyapps[1985429]:   .default = col_double(),
2020-03-24T14:41:09.786949+00:00 shinyapps[1985429]:   `Country/Region` = col_character()
2020-03-24T14:41:09.786949+00:00 shinyapps[1985429]: )
2020-03-24T14:41:09.787306+00:00 shinyapps[1985429]: See spec(...) for full column specifications.
2020-03-24T14:41:09.826539+00:00 shinyapps[1985429]: Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
2020-03-24T14:41:09.826541+00:00 shinyapps[1985429]:   GDAL Error 4: Unable to open /srv/connect/apps/covid_dashboard/data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shx or /srv/connect/apps/covid_dashboard/data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.SHX.Try --config SHAPE_RESTORE_SHX true to restore or create it
2020-03-24T14:41:09.829788+00:00 shinyapps[1985429]: Warning: Error in : Cannot open "/srv/connect/apps/covid_dashboard/data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
2020-03-24T14:41:09.837934+00:00 shinyapps[1985429]:   114: <Anonymous>
2020-03-24T14:41:09.874249+00:00 shinyapps[1985429]: 

I have tried the following to resolve this issue, but haven't found a solution:

  1. Using different functions to read in the shape file: readOGR() instead of st_read() - No change.
  2. Placing the .Rmd file in the same directory as the shape file, and hosting that - No change.
  3. Altering the code to download, unzip and open the shape file independently - Ran into issues accessing the correct directory.

Is there anything else I can try? Is there something simple that I'm missing. Let me know if I should add more detail to the code, or if I can clarify anything.


Solution

  • I discovered that st_read() may not always work well within a shiny environment:

    Adding a linestring by st_read in Shiny/Leaflet

    Shiny leaflet click-event returns NULL when clicked again

    In order to solve my problem, I read in the shape file locally using st_read(), saved it as an .RData file and then loaded that .RData file into the app to use. Below is an example:

    countries <-st_read("data/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp", stringsAsFactors=FALSE, quiet=TRUE)
    save(countries, file="./shapeFile.RData")
    load("shapeFile.RData")
    

    I uploaded the .RData file to shinyapps.io when hosting the dashboard, and it now appears to be working without that error.