ShinyFiles and shapefiles?


Has anyone managed to use the package ShinyFiles for uploading shapefiles?

I need to upload shapefiles into my shiny app (as a web-based application) without needing the user to set their working directory.

Any advice available please?

I've tried using this approach:

But unable to get this to work with rgdal.

Many thanks

What exactly do you mean by "shapefiles"?

There are multiple options for storing spatial information. Some lend themselves for shiny import better than others.

The eponymous ESRI shapefile, spread over multiple files, is a known troublemaker - but it is comparatively easier to transfer a GeoJSON or a GeoPackage, both of which are contained in single file. The GeoJSON is a favorite of CS people (web developers and such), GeoPackage is popular with GIS folks (it works sort of like the original shp on steroids, with sqlite in place of dBase IV ).

I have also integrated in shiny upload of a {sf} package object stored as a RDS file and even an Excel file with lat-lon coordinates (points are easy).

Hello @jlacko - thanks for the quick response.

By shapefile, I mean the ESRI shapefile that you highlight in your response. I have had trouble working with this, as you say, because multiple files are needed. Do you have any suggestions specifically for ESRI shapefile uploads for shiny users?

Many thanks

Hi there,
as reported here i used a simple fileInput to upload a single ESRI Shapefile and visualize that.

I don't have a solution yet to upload more than one ESRI Shapefile, but maybe you can use a numericinput to understand how many ESRI files users/you need to upload and then use a renderUI to allow the users/you to upload many ESRI shapefile you need.

it is rudimental solution but could be used.


ui <- fluidPage(
  titlePanel("Shapefile example"),
      fileInput("filemap", "", accept=c('.shp','.dbf','.sbn','.sbx','.shx',".prj"), multiple=TRUE)

server <- function(input, output) {    
    shpdf <- input$filemap
    previouswd <- getwd()
    uploaddirectory <- dirname(shpdf$datapath[1])
    for(i in 1:nrow(shpdf)){
      file.rename(shpdf$datapath[i], shpdf$name[i])
    map <- readOGR(paste(uploaddirectory, shpdf$name[grep(pattern="*.shp$", shpdf$name)], sep="/"))#,  delete_null_obj=TRUE)
    map <- spTransform(map, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))

shinyApp(ui = ui, server = server)


Thanks so much for your time. The code looks great and logical. I've had a go at using it in Rstudio, but it does not work unfortunately, I still get the "Warning: Error in ogrListLayers: Cannot open data source".

The app then crashes and highlights R#55, which is the:
map <- readOGR(paste(uploaddirectory, shpdf$name[grep(pattern="*.shp$", shpdf$name)], sep="/"))#, delete_null_obj=TRUE)

Do you have any suggestions for this?

Many thanks,

The code works only if you import 4 files of different format but they must refer to the same object

the 4 files you need to upload are shown in the following image


and you have to import all togheter as reported on the script on the fileinput function where I inserted as argument multiple=TRUE

these files could be generate by using a GIS software as Qgis.

let me know if it works

1 Like

Excellent! Thanks so much for you help. I just got it to work.

I modified it slightly as I have some extra associated files i.e.
fileInput("filemap", "", accept=c('.shp','.dbf','.sbn','.sbx','.shx','.prj','.cpg'), multiple=TRUE)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

The most common, and most difficult format to work with. Makes sense, in a wicked way...

Are you in position to ask your users to upload a single zipped file? This could be unzipped someplace, e.g. in tempdir(), and read from there - would this work for your use case?

Also, sometimes sf::st_read() works more predictably than rgdal::readOGR(); testing is of course required.