R-从闪亮的应用程序下载shapefile
我被难住了,不知道如何用shapefile的下载按钮来设置我闪亮的应用程序 我在这里查阅了参考资料 这就是我得到的:R-从闪亮的应用程序下载shapefile,r,shiny,gis,spatial,shapefile,R,Shiny,Gis,Spatial,Shapefile,我被难住了,不知道如何用shapefile的下载按钮来设置我闪亮的应用程序 我在这里查阅了参考资料 这就是我得到的: ui <- fluidPage( sidebarLayout( sidebarPanel( textInput("downloadShp","Filename:",value="fbCrawlExport.zip"), downloadButton('fbCrawlExport.zip', 'DownloadSHP')
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("downloadShp","Filename:",value="fbCrawlExport.zip"),
downloadButton('fbCrawlExport.zip', 'DownloadSHP')
)))
server <- function(input, output) {
output$fbCrawlExport.zip <- downloadHandler(
filename = 'fbCrawlExport.zip',
content = function(file) {
if (length(Sys.glob("fbCrawl.*"))>0){
file.remove(Sys.glob("fbCrawl.*"))
}
proj4string(Fg_filt2) <- CRS("+proj=longlat +datum=WGS84")
writeOGR(Fg_filt2, dsn="fbCrawl.shp", layer="fbCrawl", driver="ESRI Shapefile")
write.csv(as.data.frame(cbind(Fg_filt2@data, as.data.frame(Fg_filt2()@coords))), "fbCrawl.csv")
zip(zipfile='fbCrawlExport.zip', files=Sys.glob("fbCrawl.*"))
file.copy("fbCrawlExport.zip", file)
if (length(Sys.glob("fbCrawl.*"))>0){
file.remove(Sys.glob("fbCrawl.*"))
}
}
)
ui您的操作系统不知道如何使用zip命令(状态127)。在Unix系统上,此解决方案应该可以工作。在Windows上,您必须下载zip程序或将\bin文件夹附加到系统变量中。(可以是C:\RBuildTools\3.4\bin或C:\RBuildTools\bin或安装它的任何位置)
您可以通过打开命令提示符进行检查,然后输入zip
。当你收到一条错误信息时,说明出了问题,如果你看到其他选项,你就很好了
重新启动R并尝试以下代码
运行代码,在浏览器中打开ShinyApp并查看它是否有效
library(shiny); library(leaflet)
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))); Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5))); Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1"); Srs2 = Polygons(list(Sr2), "s2"); Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
SpDF <- SpatialPolygonsDataFrame(SpP, data.frame(ID = 1:3, p = runif(3,5,10), name=c("a","b","c")), match.ID = F)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles("http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
addPolygons(data=SpDF)
})
output$ShapeExport <- downloadHandler(
filename = 'ShapeExport.zip',
content = function(file) {
if (length(Sys.glob("shape_export.*"))>0){
file.remove(Sys.glob("shape_export.*"))
}
writeOGR(SpDF, dsn="shape_export.shp", layer="shape_export", driver="ESRI Shapefile", overwrite_layer = T)
write.csv(as.data.frame(SpDF@data), "shape_export.csv")
zip(zipfile='ShapeExport.zip', files=Sys.glob("shape_export.*"))
file.copy("ShapeExport.zip", file)
if (length(Sys.glob("shape_export.*"))>0){
file.remove(Sys.glob("shape_export.*"))
}
}
)
}
ui <- fluidPage(
downloadButton('ShapeExport', 'DownloadSHP'),
leafletOutput("map")
)
shinyApp(ui, server)
库(闪亮);图书馆(单张)
Sr1=多边形(cbind(c(2,4,4,1,2),c(2,3,5,4,2));Sr2=多边形(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3=多边形(cbind(c(4,4,5,10,4),c(5,3,2,5,5));Sr4=多边形(cbind(c(5,6,6,5,5),c(4,4,3,3,4)),孔=真)
Srs1=多边形(列表(Sr1),“s1”);Srs2=多边形(列表(Sr2),“s2”);Srs3=多边形(列表(Sr3、Sr4),“s3/4”)
SpP=空间多边形(列表(Srs1、Srs2、Srs3),1:3)
SpDF(0){
删除(Sys.glob(“shape\u export.*))
}
}
)
}
用户界面
proj4string(myshp) <- CRS("+proj=longlat +datum=WGS84")
writeOGR(myshp, "C:/Data/ShinyApps", "myshp_wgs84", driver = "ESRI Shapefile")
library(shiny); library(leaflet)
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))); Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5))); Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1"); Srs2 = Polygons(list(Sr2), "s2"); Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
SpDF <- SpatialPolygonsDataFrame(SpP, data.frame(ID = 1:3, p = runif(3,5,10), name=c("a","b","c")), match.ID = F)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles("http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
addPolygons(data=SpDF)
})
output$ShapeExport <- downloadHandler(
filename = 'ShapeExport.zip',
content = function(file) {
if (length(Sys.glob("shape_export.*"))>0){
file.remove(Sys.glob("shape_export.*"))
}
writeOGR(SpDF, dsn="shape_export.shp", layer="shape_export", driver="ESRI Shapefile", overwrite_layer = T)
write.csv(as.data.frame(SpDF@data), "shape_export.csv")
zip(zipfile='ShapeExport.zip', files=Sys.glob("shape_export.*"))
file.copy("ShapeExport.zip", file)
if (length(Sys.glob("shape_export.*"))>0){
file.remove(Sys.glob("shape_export.*"))
}
}
)
}
ui <- fluidPage(
downloadButton('ShapeExport', 'DownloadSHP'),
leafletOutput("map")
)
shinyApp(ui, server)