Skip to content

Instantly share code, notes, and snippets.

@RCura
Created May 20, 2014 10:29
Show Gist options
  • Save RCura/f5027b41f83e83d3bc28 to your computer and use it in GitHub Desktop.
Save RCura/f5027b41f83e83d3bc28 to your computer and use it in GitHub Desktop.
Shiny / R : csv to shapefile
require(shiny)
require(sp)
require(rgdal)
runApp(
list(
ui = bootstrapPage(
fileInput('inputdata', 'Input file',accept=c('.csv')),
plotOutput("xyPlot"),
downloadButton('downloadShp', 'DownloadSHP')
),
server = function(input, output) {
createShp <- reactive({
myXY <- input$inputdata
if (is.null(myXY)){
return(NULL)
} else {
xyPoints <- read.table(myXY$datapath, sep=",", header=T)
SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data = xyPoints)
#proj4string(SHP) <- CRS('+proj=longlat +datum=NAD83')
return(SHP)
}
})
output$xyPlot <- renderPlot({
plot(createShp())
})
output$downloadShp <- downloadHandler(
filename = 'shpExport.zip',
content = function(file) {
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
writeOGR(createShp(), dsn="shpExport.shp", layer="shpExport", driver="ESRI Shapefile")
zip(zipfile='shpExport.zip', files=Sys.glob("shpExport.*"))
file.copy("shpExport.zip", file)
if (length(Sys.glob("shpExport.*"))>0){
file.remove(Sys.glob("shpExport.*"))
}
}
)
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment