Can't loop with R's leaflet package to produce multiple maps

后端 未结 2 536
心在旅途
心在旅途 2020-12-01 04:19

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 tryi

相关标签:
2条回答
  • 2020-12-01 04:33

    An updated version of @timelyportfolio's post from last year. I needed to add a timelineControl function to get it to work. I also needed to call the getMap() function of the leaflet element to get the functions to bind to the map object.

    library(htmlwidgets)
    library(htmltools)
    library(leaflet)
    library(geojsonio)
    
    #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" = do.call(
            "as.Date",
            list(
                x = c("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"
            )
        )
    )
    
    # set start same as end
    #  adjust however you would like
    power$end <- power$start + 30
    
    
    # use geojsonio to convert our data.frame
    #  to GeoJSON which timeline expects
    power_geo <- geojson_json(power,lat="Latitude",lon="Longitude", pretty = T)
    
    # create a leaflet map on which we will build
    leaf <- leaflet() %>%
        addTiles()
    
    # add leaflet-timeline as a dependency
    #  to get the js and css
    leaf$dependencies[[length(leaf$dependencies)+1]] <- htmlDependency(
        name = "leaflet-timeline",
        version = "1.0.0",
        src = c("href" = "http://skeate.github.io/Leaflet.timeline/"),
        script = "javascripts/leaflet.timeline.js",
        stylesheet = "stylesheets/leaflet.timeline.css"
    )
    
    # use the new onRender in htmlwidgets to run
    #  this code once our leaflet map is rendered
    #  I did not spend time perfecting the leaflet-timeline
    #  options
    leaf %>%
        setView(44.0665,23.74667,2) %>%
        onRender(sprintf(
            '
            function(el,x){
            var power_data = %s;
    
            var timelineControl = L.timelineSliderControl({
              formatOutput: function(date) {
                return new Date(date).toString();
              }
            });
    
            var timeline = L.timeline(power_data, {
            pointToLayer: function(data, latlng){
            var hue_min = 120;
            var hue_max = 0;
            var hue = hue_min;
            return L.circleMarker(latlng, {
            radius: 10,
            color: "hsl("+hue+", 100%%, 50%%)",
            fillColor: "hsl("+hue+", 100%%, 50%%)"
            });
            },
            steps: 1000,
            duration: 10000,
            showTicks: true
            });
            timelineControl.addTo(HTMLWidgets.find(".leaflet").getMap());
            timelineControl.addTimelines(timeline);
            timeline.addTo(HTMLWidgets.find(".leaflet").getMap());
            }
            ',
            power_geo
        ))
    
    0 讨论(0)
  • 2020-12-01 04:36

    Here is a quick way to add leaflet-timeline in the way you suggest. For some reason, the timeline does not render perfectly in RStudio Viewer, but it does seem to work correctly in Chrome. I commented inline in the code to describe the steps.

    library(htmlwidgets)
    library(htmltools)
    library(leaflet)
    library(geojsonio)
    
    #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" = do.call(
        "as.Date",
        list(
          x = c("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"
        )
      )
    )
    
    # set start same as end
    #  adjust however you would like
    power$end <- power$start
    
    
    # use geojsonio to convert our data.frame
    #  to GeoJSON which timeline expects
    power_geo <- geojson_json(power,lat="Latitude",lon="Longitude")
    
    # create a leaflet map on which we will build
    leaf <- leaflet() %>%
      addTiles()
    
    # add leaflet-timeline as a dependency
    #  to get the js and css
    leaf$dependencies[[length(leaf$dependencies)+1]] <- htmlDependency(
      name = "leaflet-timeline",
      version = "1.0.0",
      src = c("href" = "http://skeate.github.io/Leaflet.timeline/"),
      script = "javascripts/leaflet.timeline.js",
      stylesheet = "stylesheets/leaflet.timeline.css"
    )
    
    # use the new onRender in htmlwidgets to run
    #  this code once our leaflet map is rendered
    #  I did not spend time perfecting the leaflet-timeline
    #  options
    leaf %>%
      setView(44.0665,23.74667,2) %>%
      onRender(sprintf(
        '
    function(el,x){
        var power_data = %s;
    
        var timeline = L.timeline(power_data, {
          pointToLayer: function(data, latlng){
            var hue_min = 120;
            var hue_max = 0;
            var hue = hue_min;
            return L.circleMarker(latlng, {
              radius: 10,
              color: "hsl("+hue+", 100%%, 50%%)",
              fillColor: "hsl("+hue+", 100%%, 50%%)"
            });
          },
          steps: 1000,
          duration: 10000,
          showTicks: true
        });
        timeline.addTo(HTMLWidgets.find(".leaflet"));
    }
        ',
        power_geo
    ))
    
    0 讨论(0)
提交回复
热议问题