由于我是R和Shiny的新手,所以我想问一些有关Shiny应用程序多层的问题。感谢您的帮助,因为我不确定我是否走对了路。
我正在尝试构建一个交互式地图应用程序,该应用程序将显示以下图层:0.基本地图(OSM),1。旧多边形,2。新多边形(特定地理区域)。
下面您可以找到我的代码:
#IMPORTED LIBRARIES
library(leaflet)
library(shinyWidgets)
require(shiny)
require(shinythemes)
library(leaflet.extras)
library(htmlwidgets)
library(jsonlite)
library(RJSONIO)
library(shinyBS)
#UI
ui <- navbarPage("Interactive map",id="nav",
tabPanel("Interactive map of this area",
div(class="outer",
tags$head(
# Include our custom CSS
includeCSS("C:/Users/Korisnik/Desktop/R/30012019_int_mapa/www/styles.css")),
leafletOutput("m",height= "100%", width="100%"))),
absolutePanel( id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
width = 330, height = "auto",
h2("Search polygons by number"),
searchInput(
inputId = "search", label = "write the number og parcel",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "350px"),
checkboxGroupInput("layers", label = h3("Choose layer"),
choices = list("Old polygons", "New polygons"),
selected = NULL),
hr(),
uiOutput("tab")
)
)
#SERVER
old_polygons<-fromJSON("old_polygons_wgs84.geojson")
new_polygons<-fromJSON("new_polygons_wgs84.geojson")
server <- function(input, output) {
output$m <- renderLeaflet({
leaflet() %>%
addTiles(group = "OSM (default)") %>%
setView(lng =#from file , lat=#from file, zoom = 11.9)%>%
addMeasure(
position = "bottomleft",
primaryLengthUnit = "meters",
primaryAreaUnit = "sqmeters",
activeColor = "#3D535D",
completedColor = "#7D4479")%>%
addFullscreenControl( pseudoFullscreen = TRUE)%>%
addResetMapButton()
})
output$layers<-({leafletProxy(m)%>%
if(input$layers=="Old polygons"){
addGeoJSON(old_polygons)%>%
showGroup("Old polygons")%>%
hideGroup("New polygons")%>%
}
})
output$res <- renderPrint({
input$search })
url <- a("Find me the price", href="http://....")
output$tab <- renderUI({
tagList("In this page you can find a price for your parcel", url)
})
}
# Run the application
shinyApp(ui = ui, server = server)
因此,我要实现的目标是,当用户在应用程序面板上单击Groupbox中的“旧多边形”时,有机会看到叠加在基本OSM地图上的多边形。或者,当用户希望看到“新多边形”时也是如此。
例如,我尝试使用之前在代码中编写的“ if语句”编写代码,但是当我尝试运行App并单击其中一层时,我只会看到基本的OSM映射。多边形未加载。
我尝试过的另一件事是在服务器端与renderLeaflet函数一起添加多边形,但是当我运行app时,它花费了很多时间来加载,最后,它根本没有加载。我只是在等待,应用程序运行没有任何结果。
在此之前,我使用shapefile而不是geoJSON成功制作了这种交互式地图,但是当我尝试在应用程序环境中执行相同的操作时,我就失败了。
下面的地图代码:
old_polygons <- readOGR(dsn = "C:/Users/Korisnik/Desktop/R/30012019_int_mapa/old_polygons_wgs84.shp")
new_polygons <- readOGR(dsn = "C:/Users/Korisnik/Desktop/R/30012019_int_mapa/new_polygons_wgs84.shp")
m<-leaflet() %>%
addTiles(group = "OSM (default)") %>%
setView(lng =, lat=, zoom = 11.9)%>%
addPolygons(data=old_polygons,
color="#cc0000",
weight=1,
smoothFactor=0.5,
group="Old polygons")%>%
addPolygons(data=new_polygons,
color="#bfff80",
weight=1,
smoothFactor=0.5,
group="New polygons")%>%
addLayersControl(
baseGroups = c("OSM (default)"),
overlayGroups = c("Old polygons", "New polygons"),
position="bottomleft",
options = layersControlOptions(collapsed = FALSE))%>%
addResetMapButton()%>%
addMeasure(
position = "topright",
primaryLengthUnit = "meters",
primaryAreaUnit = "sqmeters",
activeColor = "#3D535D",
completedColor = "#7D4479")
m
如果有人可以向我提供一些技巧,建议和帮助,以帮助您正确控制在Shiny应用程序中显示/隐藏图层,我将不胜感激。