问题描述
这里是新的,对 R 来说也相对较新,所以请先原谅我,让我知道我在这篇文章中做错了什么,以免将来惹恼其他人:
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:
我正在尝试创建一个传单地图序列(1971 年 9 月至 1972 年 4 月).最后,我想将它们加工成闪亮的,并让用户播放/暂停动画(闪亮的循环动画滑块).
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).
没有适合我的 while 和 for 循环.当我在运行代码后检查我的 i
时,增量起作用了,而传单的功能却没有.如果没有循环,我的Dynamic Leaflet Fails"(参见下面的代码部分)工作并打开了一张地图.
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 }}
推荐答案
这里有一个按你建议的方式添加 leaflet-timeline
的快速方法.出于某种原因,时间线在 RStudio Viewer 中无法完美呈现,但在 Chrome 中似乎可以正常工作.我在代码中注释了内联以描述这些步骤.
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
))
这篇关于无法使用 R 的传单包循环生成多个地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!