I have a shapefile stored in a Dropbox folder that I would like to display in a Shiny App. I am able to download the .zip file from the Dropbox download URL, unzip the file, then read the .shp file using st_read() from the sf package. This method works when I run the Shiny app locally in R, but not when the app is deployed on shiny.myorganization.org.
I tried reading the shapefile directly from Dropbox into st_read() via the Dropbox share URL, but received the error, Cannot open "URL"; The file doesn't seem to exist. I receive the same error when trying to read the Dropbox share URL for the .zip folder, the unzipped folder, and the .shp file directly.
Is the issue that my code below works in R but on the deployed Shiny app because the files need to be downloaded and unzipped? Is there a way to bypass this? Is there something I am missing on the Shiny side; perhaps a missing package or permission?
I want to store the .shp file in Dropbox, not in the GitHub repository where the Shiny app is stored. I thought I found a potential solution in this post but the rdrop2 package it uses is no longer supported.
Thank you.
# load libraries
library(sf)
library(leaflet)
library(shiny)
# load data
# download folder from dropbox that contains the shapefile
aFile<- "https://www.dropbox.com/myshapefiletodownload/key=1"
download.file(url = aFile, destfile = "myshapefiletodownload.dir", mode = "wb")
# unzip the folder to see the files
unzip("myshapefiletodownload.dir")
# read in the shapefile
df<- st_read(dsn = "myshapefile.shp", layer = "df")
# define UI
ui<- fluidPage(
mainPanel(leafletOutput("map"))
)
# Define server logic
server<- function(input, output) {
output$map<- renderLeaflet({
leaflet(df) %>%
addTiles() %>%
addPolygons() %>%
})
}
# Run the app
shinyApp(ui, server)
sfusesGDALfor reading data, which means you can use GDAL Virtual File Systems withsf-- assuming you have a working direct download link for a Shapefile archive, prefix it with"/vsizip//vsicurl/"to let GDAL handle downloading and extraction:Created on 2024-03-12 with reprex v2.1.0