【问题标题】:R - shiny - addPolygons must be called with both lng and lat, or with neitherR - shiny - addPolygons 必须同时使用 lng 和 lat 调用,或者两者都不使用
【发布时间】:2020-06-17 01:13:22
【问题描述】:

我想在这里寻求帮助。 我有我的多边形 shapefile,我想在 R-shiny 中通过传单显示它。但是,我一直得到结果:错误:addPolygons 必须同时使用 lng 和 lat 调用,或者两者都不使用。

我已经尝试过使用 sp 包和 sf 包(如下链接:https://community.rstudio.com/t/projection-problems-with-leaflet/27747/5),并用这两个包更改了 CRS。在闪亮的外面它工作正常,但在它里面就是不工作。

library(rgdal)
library(sp)
library(leaflet)

wo <-readOGR("SHAPES/world/ne_50m_admin_0_countries.shp", layer = "ne_50m_admin_0_countries")
wt <- spTransform(wo, CRS("+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs"))


ui <- fluidPage(

    # Application title
    titlePanel("mytitle"),

        mainPanel(
           leafletOutput("map", width = "100%", height = 600)
        )
    )
)


server <- function(input, output) {

    output$map <- renderLeaflet({
       leaflet(wt) %>%
            addTiles(group = "OpenStreetMap") %>%
            addProviderTiles("CartoDB.Positron", group = "CartoDB")%>%
            addProviderTiles("Esri.WorldImagery", group = "ESRI")%>%
            setView(lng= -78.49511705 , lat = -1.47200601, zoom = 5)%>%
            addMiniMap(tiles = providers$CartoDB.Positron,
                        toggleDisplay = TRUE) %>%
            addLayersControl(baseGroups = c("OpenstreetMap", "CartoDB", "ESRI"))%>%
            addPolygons(color = "white", weight = 1, smoothFactor = 0.5, 
                        opacity = 1,0, fillOpacity = 0.5
                        #dashArray = "3",
                         #highlightOptions = highlightOptions(color = "grey", weight =2, 
                         #bringToFront = TRUE)
                        )
               })

}

# Run the application 
shinyApp(ui = ui, server = server)

我还尝试在 addPolygons 中显示变量,如下所示:

addPolygons(data = wt, color = "white", weight = 1, smoothFactor = 0.5, 
                        opacity = 1,0, fillOpacity = 0.5
                        #dashArray = "3",
                         #highlightOptions = highlightOptions(color = "grey", weight =2, 
                         #bringToFront = TRUE)
                        )

有人可以看看这里发生了什么吗?

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    在 addPolygons 中代替 data = wt ,尝试使用 data = wt ()。

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 2018-06-11
      • 2016-03-14
      • 2011-03-14
      • 2022-01-03
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多