New here and relatively new to R also, so please forgive apriori and let me know what I'm doing wrong in this post to avoid annoying others in the future:
I am trying to create a sequence (Sep-1971 to Apr-1972) of leaflet maps. In the end, I'd like to crunch them into shiny and have a user play/pause an animation (shiny looping animation slider).
No while and for loops worked for me. Increments had worked when I checked my i
s after running the code, the leaflet functions not. Without the loop, my "Dynamic Leaflet Fails" (see below in code section) worked and opened a map.
Is it not possible to create leaflets sequentially?
#set working directory
require(leaflet)
require(dplyr)
#Build data.frame with 10 obs + 3 cols
power <- data.frame(Latitude <-c(33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167, 54.140586, 54.140586, 48.494444, 48.494444), Longitude <- c(
129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889, 13.664422, 13.664422, 17.681944, 17.681944), start <- c(as.Date(
"15-Sep-1971", "1-Dec-1971", "1-Feb-1972", "1-Feb-1972", "1-Feb-1972", "1-Feb-1972", "1-Apr-1972", "1-Apr-1972", "24-Apr-1972", "24-Apr-1972", format = "%d-%b-%Y")))
#"Dynamic" leaflet Fails1: While+For combo
i<- as.Date("1971-09-14")
while (i < as.Date("1972-05-01")) { for(star in start){
if (star > i) {
leaflet(power) %>% addTiles() %>%
addCircleMarkers(lng = ~Longitude, lat = ~Latitude)
}}
i <- i+60}
#"Dynamic" leaflet Fails2: For+break combo
lap <- seq(as.Date("1971-09-14"), as.Date("1972-05-01"), by = "month")
for(i in lap) {
leaflet (data = power[power$start > i,]) %>%
addTiles() %>%
addCircleMarkers(lng = ~Longitude, lat = ~Latitude)
if (i > as.Date("1951-01-01"))
{ break }}