【问题标题】:Adding multiple layers to leaflet map in Shiny in R在R中的Shiny中向传单地图添加多个图层
【发布时间】:2017-07-29 17:51:26
【问题描述】:

我无法在闪亮的应用程序中添加不同的图层。我想添加一组多边形以及一组圆形标记以及一组任意(.png)图标。我有一组 geojson 文件,它们被添加到一个 for 循环中,该循环包含在带有函数的 observe({}) 语句中 map$addGeoJSON(x) 其中 x 是具有坐标的特征。 'map' 对象由命令创建

map <- createLeafletMap(session, 'map')

这一切都很好,而且多边形也很好。我也想采用这种添加多边形的方式。这不应该改变。

当我尝试以相同的方式(例如,使用 map$addMarkers(....) )向该地图对象添加标记时会发生错误 下面是错误和尝试在所需的方式并失败。 下面带有地震数据的闪亮应用重现了我的错误

"Listening on ...
Warning: Error in observerFunc: attempt to apply non-function
Stack trace (innermost first):
    56: observerFunc [C:/Users/jbz/Desktop/leaflet-map-question.R#35]
     1: runApp
ERROR: [on_request_read] connection reset by peer"

library(shiny)
library(leaflet)
library(RColorBrewer)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletMap("map", width = "100%", height = "100%",
             options=list(center = c(40.736, -73.99), zoom = 14)),
  absolutePanel(top = 10, right = 10,
                sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
                            value = range(quakes$mag), step = 0.1
                ),
                selectInput("colors", "Color Scheme",
                            rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                ),
                checkboxInput("legend", "Show legend", TRUE)
  )
)

server <- function(input, output, session) {

  filteredData <- reactive({
    quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
  })

  colorpal <- reactive({
    colorNumeric(input$colors, quakes$mag)
  })


  map <- createLeafletMap(session, 'map')

  observe({
    df <- filteredData()
    map$addMarkers(
      lng=df$Lon, lat=df$Lat, popup = paste(as.character(df$mag)))
  })
}

shinyApp(ui, server)

(如何)在坚持使用函数createLeafletMap()的同时正确添加标记?

map <- createLeafletMap(session, 'map')

【问题讨论】:

  • 你也可以提供你的createLeafletMap()函数吗?
  • createLeafletMap 已弃用
  • 案例错误:lng=df$lon, lat=df$lat
  • 试图复制rpubs.com/Sylvie/cityBikeApp上的结构

标签: r shiny leaflet polygon


【解决方案1】:

尝试:

library(dplyr)
df <- filteredData()
leafletProxy("map") %>%
addMarkers(df, lng = ~Lon, lat = ~Lat, popup = paste(as.character(df$mag) )

observe

【讨论】:

  • OP 声明:“(如何)在坚持使用函数 createLeafletMap() 的同时正确添加标记?” - 你的答案是:否?
  • 地图仍使用createLeafletMap() 创建。 renderLeaflet 会有所不同。
  • 警告:UseMethod 中的错误:“元数据”没有适用的方法应用于“NULL”类的对象
  • 得到与 HubertL 相同的错误。 UseMethod("doResolveFormula") 中的错误:没有适用于 'doResolveFormula' 的方法应用于“NULL”类的对象
  • 这里有同样的错误。我认为某处需要if(!is.null(x)){do something}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2016-10-14
  • 2017-02-08
  • 2022-08-15
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多