2016-08-11 3 views
1

Ich möchte einige Plot-Funktionen mit Hilfe von Broschüren anpassen. Mein Problem ist, dass die Karten nicht im HTML angezeigt werden, sobald ich das Flugblatt in einer Funktion verwende. Kennt anybod eine Lösung?Warum wird eine Karte nicht angezeigt, wenn ich in einer Funktion den Flyer anrufe

Hier ist ein Beispiel, und was ich habe versucht:

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) 
} 
``` 

Einer dieser Handlung sollte irgendwie funktionieren. Was ich versucht:

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) 
``` 

Ohne eine Funktion, wird alles angezeigt:

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) 
``` 
+0

Sie brauchen ass ignment ('<-'), nicht piping ('%>% ') bei Verwendung von return in einer Funktion. Siehe meine Antwort unten. – TimSalabim

Antwort

3

Sie müssen den addCircleMarkers Anruf an Ihre Karte m in 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) 
} 

zuweisen Dann werden die Marker werden beim Aufruf angezeigt werden map_poa()

Verwandte Themen