让R脚本自动创建和更新传单地图/ Rpubs或闪亮的应用程序

时间:2018-07-09 20:45:04

标签: r shiny leaflet r-markdown knitr

我正在使用卫星遥测装置跟踪鸟类,并且正在使用传单地图和RPubs可视化/共享数据。我每周下载1-2次遥测数据,运行下面的代码以创建传单地图,然后按“发布”按钮,并按照步骤在RPubs上发布地图。我想在代码中自动执行此操作,因此,当我每周添加新数据并重新运行代码时,它每次都会更新同一RPubs页面,从而向地图添加新位置。这是我当前的设置(此处是RPubs链接:http://rpubs.com/jmwiniarski/trackingmap)。

# 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

是否可以在代码中使用“ rpubsUpload”功能来自动创建/更新传单地图和rpubs网站,而不是每次都按“发布”按钮?如果是这样,那需要是一个.Rmd文件,对吗?还是最好用闪亮的应用程序完成?

0 个答案:

没有答案