gpt4 book ai didi

r - 无法使用 R 的 leaflet 包循环生成多个 map

转载 作者:行者123 更新时间:2023-12-02 20:56:42 24 4
gpt4 key购买 nike

这里是新人,对 R 来说也相对较新,所以请原谅 apriori 并让我知道我在这篇文章中做错了什么,以避免将来烦扰其他人:

我正在尝试创建传单 map 序列(1971 年 9 月至 1972 年 4 月)。最后,我想将它们压缩成 Shiny 的,并让用户播放/暂停动画( Shiny 的循环动画 slider )。

while 和 for 循环对我来说不起作用。当我运行代码后检查我的 i 时,增量起作用了,但传单不起作用。如果没有循环,我的“动态传单失败”(请参阅​​下面的代码部分)可以工作并打开 map 。

是否无法按顺序创建传单?

#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 }}

最佳答案

这是按照您建议的方式添加传单时间线的快速方法。由于某种原因,时间线在 RStudio Viewer 中无法完美呈现,但在 Chrome 中似乎可以正常工作。我在代码中内嵌注释来描述步骤。

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 - 无法使用 R 的 leaflet 包循环生成多个 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com