Read shapefile from Dropbox URL into Shiny App in R?

46 Views Asked by At

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)
1

There are 1 best solutions below

0
margusl On

sf uses GDAL for reading data, which means you can use GDAL Virtual File Systems with sf -- assuming you have a working direct download link for a Shapefile archive, prefix it with "/vsizip//vsicurl/" to let GDAL handle downloading and extraction:

url_ <- "https://raw.githubusercontent.com/OSGeo/gdal/master/autotest/ogr/data/shp/poly.zip"
sf::read_sf(paste0("/vsizip//vsicurl/", url_))

#> Simple feature collection with 10 features and 3 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 478315.5 ymin: 4762880 xmax: 481645.3 ymax: 4765610
#> Projected CRS: OSGB36 / British National Grid
#> # A tibble: 10 × 4
#>        AREA EAS_ID PRFEDEA                                              geometry
#>       <dbl>  <dbl> <chr>                                           <POLYGON [m]>
#>  1  215229.    168 35043411 ((479819.8 4765181, 479690.2 4765260, 479647 476537…
#>  2  247328.    179 35043423 ((480035.3 4765559, 480039 4765540, 479730.4 476540…
#>  3  261753.    171 35043414 ((479819.8 4765181, 479859.9 4765270, 479909.9 4765…
#>  4  547597.    173 35043416 ((479014.9 4765148, 479029.7 4765111, 479117.8 4764…
#>  5   15776.    172 35043415 ((479029.7 4765111, 479046.5 4765117, 479123.3 4765…
#>  6  101430.    169 35043412 ((480083 4765050, 480080.3 4764980, 480134 4764857,…
#>  7  268598.    166 35043409 ((480389.7 4764950, 480537.2 4765014, 480568 476491…
#>  8 1634833.    158 35043369 ((480701.1 4764738, 480761.5 4764778, 480825 476482…
#>  9  596610.    165 35043408 ((479750.7 4764702, 479968.5 4764788, 479985.1 4764…
#> 10    5269.    170 35043413 ((479750.7 4764702, 479658.6 4764670, 479640.1 4764…

Created on 2024-03-12 with reprex v2.1.0