Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为什么在函数中调用传单时不显示地图_R_Leaflet - Fatal编程技术网

R 为什么在函数中调用传单时不显示地图

R 为什么在函数中调用传单时不显示地图,r,leaflet,R,Leaflet,我想使用传单自定义一些绘图函数。我的问题是,在函数中使用传单时,地图不会立即显示在html中。有人知道解决办法吗 下面是一个例子,也是我尝试过的: Save this as .Rmd ```{r, c1, echo = FALSE} map <- function() { library(leaflet) m <- leaflet(width = "100%") %>% addTiles(group = "OSM") %>% addProvider

我想使用传单自定义一些绘图函数。我的问题是,在函数中使用传单时,地图不会立即显示在html中。有人知道解决办法吗

下面是一个例子,也是我尝试过的:

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, 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)
```
将此块添加到上面的.Rmd
```{r,c3,echo=FALSE}
m%添加循环标记(lat=c(47,47.5),
液化天然气=c(8,8.5),
笔划=假,
半径=10,
填充不透明度=0.8,
clusterOptions=markerClusterOptions())
打印(m)
```

您需要在
map\u poa()


map\u poa您需要在
map\u poa()中将
addCircleMarkers
调用分配给map
m

map\u poa在函数中使用return时需要分配(
%
)。请参阅下面我的答案。在函数中使用return时,需要分配(
%
)。见下面我的答案。
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)
```
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)
}