使用htmltools在传单地图中创建标签会产生微小的标签

时间:2019-04-27 05:42:37

标签: html r leaflet

创建传单地图。第一步,指定标签。传单github上使用的代码

%>% lapply(htmltool::HTML) 
sprintf()函数之后的

。但是,这样做是将标签创建为类型:“列表”,从而导致错误:“ sum(sapply(label,function(x){错误:参数的“类型”(列表)无效”

因此,为了解决这个问题,我只加载了htmltools库并使用代码

HTML(sprintf(...)) 

这样做可以运行并运行地图,但是标签显示为没有信息的小盒子(请参见下面的图片链接)

我无法确定这是否与sprintf()中的代码有关,还是与HTML()有关。

奇怪的是,%>%lapply方法可以正常工作,但是发生了一些事情,现在出现了上面提到的错误

Image with the small label shown as little white box

labels.dest2 <- sprintf("<div style = 'overflow-wrap: anywhere;'><strong>%s <br/>%s Destinations</div><br/>%s Euclidean Miles from LAX on average<br/>%s minutes between OD tweets </div><br/>%s Miles from LAX on average</div><br/>%s minutes from LAX on average</div>",
                  puma.spdf$NAME,
                  puma.spdf$Dest_pt_count,
                  puma.spdf$Avg_Euc_Dist_Mi,
                  puma.spdf$Avg_tweetTime,
                  puma.spdf$Avg_RtDist_Mi,
                  puma.spdf$Avg_RtTime_min) %>% lapply(htmltools::HTML)

leaflet() %>% addTiles() %>% etc...

此处输入完整代码

## Map with OD data and travel stats  ##

labels.dest2 <- HTML(sprintf("<div style = 'overflow-wrap: anywhere;'> <strong>%s <br/>%g Destinations</div><br/>%s Euclidean Miles from LAX on average<br/>%s minutes between OD tweets </div><br/>%s Miles from LAX on average</div><br/>%s minutes from LAX on average</div>",
                  puma.spdf$NAME,
                  puma.spdf$Dest_pt_count,
                  puma.spdf$Avg_Euc_Dist_Mi,
                  puma.spdf$Avg_tweetTime,
                  puma.spdf$Avg_RtDist_Mi,
                  puma.spdf$Avg_RtTime_min))

leaflet() %>% addTiles() %>% 
  setView(lng=-118.243683, lat=34.1, zoom  = 9.35) %>%
  addEasyButton(easyButton(
    icon="fa-crosshairs", title = "Default View", 
    onClick=JS("function(btn, map) {var groupLayer = map.layerManager.getLayerGroup('Destinations (red)'); map.fitBounds(groupLayer.getBounds());}")))  %>%
  addProviderTiles(providers$CartoDB.Positron, 
                   group = "Grey") %>%
  addProviderTiles(providers$OpenStreetMap.BlackAndWhite, 
                   group = "OSM")  %>%

  # Add Polygons
  # Destination data
    addPolygons(data = puma.spdf,
              group = "Destination Density",
              fillColor = ~pal.dest(Dest_pt_count),
              weight = 1,
              opacity = 90,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.5,
              highlight = highlightOptions(weight = 2,
                                           color = "#666",
                                           dashArray = "",
                                           fillOpacity = 0.7,
                                           bringToFront = TRUE,
                                           sendToBack = TRUE),
              label = labels.dest2,
              labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px"),
                                          textsize = "15px",
                                          direction = "auto")) %>%
  addLegend(values=puma.spdf$Dest_pt_count,
            group = "Destination Density",
            pal=pal.dest,
            title="Destination Density (Dest per PUMA)", 
            position = "bottomright") %>%

   # Add Points
  addCircleMarkers(data = D.spdf, 
                   radius = 2,
                   color = "red",
                   group = "Destinations (red)",
                   fillOpacity = 0.5) %>%
  addCircleMarkers(data = O.spdf, 
                   radius = 2,
                   color = "green",
                   group = "Origins (green)") %>%

  # Add Layer Controls
  addLayersControl(
    baseGroups = c("OSM (default)", "Grey"),
    overlayGroups = c("Destinations (red)", "Origins (green)","Destination Density"),
    options = layersControlOptions(collapsed = FALSE)
  )

1 个答案:

答案 0 :(得分:0)

问题在于,第一列puma.spdf $ NAME不属于数据集,并且已从字符串中删除。请检查以确保要显示的所有变量实际上都属于数据集。