如何使用Shiny将多个对象添加到传单地图

时间:2015-11-10 14:48:41

标签: r shiny leaflet

AIM

使用Shiny创建一个传单地图,该地图使用圆形标记表示一组数据,并使用第二组数据为点添加标记。

问题 "圈"标记正在工作,但"标记"不是。 " addMarkers"代码未被读取或被忽略。

服务器

    library(shiny)
    library(leaflet)

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

    points <- read.csv(textConnection("Loc,STZip,Lat,Long,Vol
                                Loc1,17699,40.0185,-76.297582,15
                                Loc2,76177,32.949819,-97.31406,20
                                Loc3,27801,35.935125,-77.77076,17
                                Loc4,52404,41.947335,-91.68819,12
                                Loc5,19380,39.983108,-75.59332,18
                                "))
    newpoints <- read.csv(textConnection("Loc,STZip,Lat,Long,Vol
                                 Loc6,18640,41.317242,-75.77942,12
                                   Loc7,38133,35.208709,-89.80518,20
                                "))

    output$mymap <- renderLeaflet({
    leaflet() %>%
    addProviderTiles("Stamen.TonerLite",
                   options = providerTileOptions(noWrap = TRUE)) %>%
    addCircleMarkers(lng = ~Long, lat = ~Lat, radius = ~Vol, layerId = NULL,
             group = "NGS_Facilities", stroke = TRUE, color = "#0000CC",    weight = 5, opacity = 0.5,
             fill = TRUE, fillColor = "#0000CC", fillOpacity = 0.2,   dashArray = NULL,
             popup = ~Loc, options = pathOptions(), clusterOptions = NULL, clusterId = NULL,
             data = (newpoints))  %>%

   #this code is not being read or is ignored...
   addMarkers(lng = ~Long, lat = ~Lat, popup = ~Loc, data = (newpoints))


})

}

UI

库(有光泽) 库(单张)

r_colors <- rgb(t(col2rgb(colors()) / 255))
 names(r_colors) <- colors()

ui <- fluidPage(
   title = "Map of Stuff",
   leafletOutput("mymap", width = 1800, height = 800),
   p()
)

1 个答案:

答案 0 :(得分:2)

这是一个奇怪的错误......与它争吵了一段时间,直到我意识到这是你读数据的问题。

> newpoints
                                      Loc STZip      Lat      Long Vol
1                                    Loc6 18640 41.31724 -75.77942  12
2                                    Loc7 38133 35.20871 -89.80518  20
3                                            NA       NA        NA  NA

因为您的最终报价是在一个新行,它会留下一个休息时间。这会导致数据中的最后一行为NA。当我进行调试时,它似乎就像我在数据显示之前放的任何东西,但之后会失败。

要解决此问题,请将您的数据读取为:

  points <- read.csv(textConnection("Loc,STZip,Lat,Long,Vol
                                Loc1,17699,40.0185,-76.297582,15
                                Loc2,76177,32.949819,-97.31406,20
                                Loc3,27801,35.935125,-77.77076,17
                                Loc4,52404,41.947335,-91.68819,12
                                Loc5,19380,39.983108,-75.59332,18"))
  newpoints <- read.csv(textConnection("Loc,STZip,Lat,Long,Vol
                                 Loc6,18640,40.0185,-76.297582,12
                                   Loc7,38133,35.208709,-89.80518,20"))

无论出于何种原因,如果最后一行都是NA s

,Leaflet就会出错