ShinyFiles and shapefiles?

Hi there,
as reported here https://forum.posit.co/t/downloadhandler-error-shp-file/70118/11 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.

library(shiny)
library(rgdal)
library(mapview)

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

server <- function(input, output) {    
  observe({
    shpdf <- input$filemap
    if(is.null(shpdf)){
      return()
    }
    previouswd <- getwd()
    uploaddirectory <- dirname(shpdf$datapath[1])
    setwd(uploaddirectory)
    for(i in 1:nrow(shpdf)){
      file.rename(shpdf$datapath[i], shpdf$name[i])
    }
    setwd(previouswd)
    
    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"))
    
    output$mapview<-renderMapview({
      mapview(map)
    })
  })
}

shinyApp(ui = ui, server = server)
2 Likes