让R脚本自动创建和更新传单地图/Rpubs或闪亮应用程序
我正在用卫星遥测装置跟踪鸟类,我正在使用传单地图和RPUB可视化/共享数据。我每周下载遥测数据1-2次,运行下面的代码创建一张传单地图,然后按下“发布”按钮,并完成在RPubs上发布地图的步骤。我希望在代码中自动执行此操作,因此当我每周添加新数据并重新运行代码时,每次都会更新相同的RPubs页面,向地图添加新位置。这是我当前的设置(以及此处的RPubs链接:)让R脚本自动创建和更新传单地图/Rpubs或闪亮应用程序,r,shiny,leaflet,r-markdown,knitr,R,Shiny,Leaflet,R Markdown,Knitr,我正在用卫星遥测装置跟踪鸟类,我正在使用传单地图和RPUB可视化/共享数据。我每周下载遥测数据1-2次,运行下面的代码创建一张传单地图,然后按下“发布”按钮,并完成在RPubs上发布地图的步骤。我希望在代码中自动执行此操作,因此当我每周添加新数据并重新运行代码时,每次都会更新相同的RPubs页面,向地图添加新位置。这是我当前的设置(以及此处的RPubs链接:) #加载库------------------------------------------------------ 图书馆(tidyv
#加载库------------------------------------------------------
图书馆(tidyverse)
图书馆(单张)
图书馆(单张、附加资料)
图书馆(lubridate)
图书馆(这里)
图书馆(sp)
here::here()
#用于在传单中创建行的源R脚本代码:
生产线%
不同的
match.ID=FALSE
)
}
源('makeLines.R')#创建连接连续GPS定位的线
#读入数据--------------------------------------------------------
#读取每个带有跟踪数据的.csv文件
locs%
添加多段线(
数据=生成线(LOC),
重量=3,
不透明度=0.4,
颜色=~colorPalette(TagID))%>%
addCircleMarkers(
~z~经度,
~z~纬度,
重量=1,
颜色='黑色',
fillColor=~颜色调色板(TagID),
半径=5,
填充不透明度=0.8,
弹出=粘贴(
“,”TagID:”,locs$TagID,,“
”,
“,”纬度“,”,locs$纬度“
”,
“,”长“,”,locs$经度“
”,
“,”日期“,”,locs$日期“
”,
“,”时间(GMT):“,”,locs$Time,“
”,
“,”修复类型:“,”,locs$Fix,“
”,
“”,“修复:”,“”,locs$序列))%>%
添加层控件(
位置='bottomleft',
基本组=c(“CartoDB”、“卫星”、“地形”),
选项=图层控制选项(折叠=假))%>%
addLegend(位置='bottomleft',
pal=调色板,
值=~TagID,
标签=~TagID,
title=“TagID”)
地图
是否可以使用代码中的“rpubs上载”功能自动创建/更新传单地图和rpubs网站,而不是每次按“发布”按钮?如果是这样,这将需要一个.Rmd文件,对吗?或者最好使用闪亮的应用程序?这不是对您问题的直接回答,但解决这个问题的一种方法是将您的宣传单包装到一个闪亮的应用程序中,该应用程序可以查询云SQL数据库中的表。然后,您可以将新行附加到SQL数据库中,每次应用程序启动时,它都会从数据库中提取新数据。@RichPauloo任何替代方法都会有所帮助。你知道有什么资源可以让我看看吗?
# Load the libraries ------------------------------------------------------
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(lubridate)
library(here)
library(sp)
here::here()
# code for source R script to create lines in leaflet:
makeLines <-
function(dataframeIn, colorMatch) {
# Unique IDs:
birdIDs <- unique(dataframeIn$TagID)
# List of data frames, one per ID:
tracksList <- vector('list', length = length(birdIDs))
for (i in 1:length(tracksList)) {
tracksList[[i]] <- dataframeIn %>%
filter(TagID == birdIDs[i])
}
# List of lines objects (one list item per tagId):
birdLines <- vector('list', length = length(birdIDs))
for (i in 1:length(tracksList)) {
coordMat <- tracksList[[i]] %>%
select(Longitude, Latitude) %>%
as.matrix
birdLines[[i]] <- Lines(Line(coordMat),
ID = tracksList[[i]] %>%
select(TagID) %>%
unique)
}
# Combine lines list to spatial lines data frame:
SpatialLinesDataFrame(
SpatialLines(birdLines),
data = dataframeIn %>%
select(TagID) %>%
distinct,
match.ID = FALSE
)
}
source('makeLines.R') # create lines connecting sequential GPS fixes
# Read in the data --------------------------------------------------------
# Read each .csv file with tracking data
locs <- structure(list(TagID = c("019", "019", "024", "024", "024", "024",
"025", "025", "025", "025", "025", "025", "025", "026", "026",
"026", "026", "026", "028", "028"), Date = structure(c(17704,
17706, 17706, 17708, 17710, 17714, 17559, 17579, 17587, 17599,
17609, 17615, 17627, 17559, 17571, 17573, 17575, 17593, 17698,
17716), class = "Date"), Time = structure(c(64768, 64768, 64768,
64768, 64768, 64768, 80303, 64896, 64896, 64896, 64896, 64896,
64896, 64896, 64896, 64896, 64896, 81009, 61184, 61184), class = c("hms",
"difftime"), units = "secs"), Latitude = c(56.90802, 56.9080429,
40.7270012, 40.7275391, 40.7255173, 40.7277451, 27.748, 27.7417603,
27.7414532, 27.7413254, 27.7408905, 27.7412987, 27.7706108, 27.6432247,
27.6447868, 27.6444798, 27.6433029, 27.644, 50.3811455, 50.3812485
), Longitude = c(-117.5436935, -117.5441055, -112.0785522, -112.0789871,
-112.0792694, -112.0784225, -97.125, -97.130661, -97.1301727,
-97.1277695, -97.126564, -97.1327133, -97.1152496, -97.803688,
-97.8031998, -97.8071671, -97.8036575, -97.805, -104.5258102,
-104.5260696), Fix = c("3D", "3D", "3D", "3D", "3D", "3D", "A2",
"2D", "3D", "3D", "3D", "3D", "2D", "3D", "3D", "3D", "3D", "A3",
"3D", "3D"), Sequence = c(2L, 3L, 2L, 3L, 4L, 6L, 4L, 12L, 15L,
20L, 25L, 27L, 32L, 2L, 6L, 7L, 8L, 17L, 5L, 9L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -20L), .Names = c("TagID",
"Date", "Time", "Latitude", "Longitude", "Fix", "Sequence"))
# Map in leaflet by individual ----------------------------------------------------------
TagIDs <- unique(locs$TagID)
# Set colors for birds:
colorPalette <- colorFactor(palette = rainbow(length(TagIDs)), # could also try primary.colors
domain = locs$TagID)
# Plot everything (points and tracks) in a leaflet map
map <- leaflet(locs) %>%
addProviderTiles(
"CartoDB.Positron",
group = "CartoDB") %>%
addProviderTiles(
"Esri.WorldImagery",
group = "Satellite") %>%
addProviderTiles(
"Stamen.Terrain",
group = "Terrain") %>%
addPolylines(
data = makeLines(locs),
weight = 3,
opacity = 0.4,
color = ~ colorPalette(TagID)) %>%
addCircleMarkers(
~Longitude,
~Latitude,
weight = 1,
color = 'black',
fillColor = ~ colorPalette(TagID),
radius = 5,
fillOpacity = 0.8,
popup=paste(
'<b style="color:#0000FF">', "TagID:", locs$TagID, '</b>', '<br>',
"<b>", "Lat:", "</b>", locs$Latitude, "<br>",
"<b>", "Long:", "</b>", locs$Longitude, "<br>",
"<b>", "Date:", "</b>", locs$Date, "<br>",
"<b>", "Time (GMT):", "</b>", locs$Time, "<br>",
"<b>", "Fix type:", "</b>", locs$Fix, "<br>",
"<b>", "Fix #:", "</b>", locs$Sequence)) %>%
addLayersControl(
position = 'bottomleft',
baseGroups = c("CartoDB", "Satellite", "Terrain"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(position = 'bottomleft',
pal = colorPalette,
values = ~TagID,
labels = ~TagID,
title = "TagID")
map