Skip to content

Instantly share code, notes, and snippets.

@RCura
Created September 18, 2014 20:44
Show Gist options
  • Save RCura/f3b3c7d224f1701a9d7f to your computer and use it in GitHub Desktop.
Save RCura/f3b3c7d224f1701a9d7f to your computer and use it in GitHub Desktop.
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({
validate(
need(!is.null(input$inputdata), "Please select a data set")
)
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