当我在函数中调用传单时,为什么没有显示地图

时间:2016-08-11 13:09:32

标签: r leaflet

我想使用传单自定义一些绘图函数。我的问题是,一旦我在函数中使用传单,地图就不会显示在html中。 anybod知道解决方案吗?

这是一个例子,我尝试过:

Save this as .Rmd
```{r, c1, echo = FALSE}
map <- function() {
  library(leaflet)
  m <- leaflet(width = "100%") %>%
    addTiles(group = "OSM") %>%
    addProviderTiles("Stamen.TonerLite") %>%
    addLayersControl(
      baseGroups = c("OSM", "Stamen.TonerLite"))
  return(m)
}

map_poa <- function() {
  m <- map()
  m %>% addCircleMarkers(lat = c(47, 47.5),
                         lng = c(8, 8.5),
                         stroke = FALSE,
                         radius = 10,
                         fillOpacity = 0.8,
                         clusterOptions = markerClusterOptions())
    print(m)
  return(m)
}
```

其中一个情节应该以某种方式起作用。我尝试了什么:

Add this chunk to the above .Rmd
```{r, c2, echo = FALSE}
# map() # Works
map_poa() # map without content
# print(map_poa()) # Does not work at all (Nothing shown)
# plot(map_poa()) # Does not work (Throws error)
```

没有功能,一切都会显示出来:

Add this chunk to the above .Rmd
```{r, c3, echo = FALSE}
m <- map()
m %>% addCircleMarkers(lat = c(47, 47.5),
                       lng = c(8, 8.5),
                       stroke = FALSE,
                       radius = 10,
                       fillOpacity = 0.8,
                       clusterOptions = markerClusterOptions())
print(m)
```

1 个答案:

答案 0 :(得分:4)

您需要在addCircleMarkers

中将m来电分配给您的地图map_poa()
map_poa <- function() {
  m <- map()
  # m <- m %>% addCircleMarkers( # also ok
  m <- addCircleMarkers(map = m,
                        lat = c(47, 47.5),
                        lng = c(8, 8.5),
                        stroke = FALSE,
                        radius = 10,
                        fillOpacity = 0.8,
                        clusterOptions = markerClusterOptions())

  return(m)
}

然后在调用map_poa()

时会显示标记