addPlayback: Add Playback to Leaflet

View source: R/playback.R

addPlaybackR Documentation

Add Playback to Leaflet

Description

The LeafletPlayback plugin provides the ability to replay GPS Points in the form of POINT Simple Features. Rather than simply animating a marker along a polyline, the speed of the animation is synchronized to a clock. The playback functionality is similar to a video player; you can start and stop playback or change the playback speed.

Usage

addPlayback(
  map,
  data,
  time = "time",
  icon = NULL,
  pathOpts = pathOptions(),
  popup = NULL,
  label = NULL,
  popupOptions = NULL,
  labelOptions = NULL,
  options = playbackOptions(),
  name = NULL
)

Arguments

map

a map widget

data

data must be a POINT Simple Feature or a list of POINT Simple Feature's with a time column.

time

The column name of the time column. Default is "time".

icon

an icon which can be created with makeIcon

pathOpts

style the CircleMarkers with pathOptions

popup

A formula with the column names for the popup content

label

A formula with the column names for the label content

popupOptions

A Vector of popupOptions to provide popups

labelOptions

A Vector of labelOptions to provide label options for each label. Default NULL

options

List of additional options. See playbackOptions

name

A formula with the column names for the feature name

Value

the new map object

Note

If used in Shiny, you can listen to 2 events

  • 'map-ID'+"_pb_mouseover"

  • 'map-ID'+"_pb_click"

References

https://github.com/hallahan/LeafletPlayback

See Also

Other Playback Functions: playbackOptions(), removePlayback()

Examples

library(leaflet)
library(leaflet.extras2)
library(sf)

## Single Elements
data <- sf::st_as_sf(leaflet::atlStorms2005[1, ])
data <- st_cast(data, "POINT")
data$time <- as.POSIXct(
  seq.POSIXt(Sys.time() - 1000, Sys.time(), length.out = nrow(data))
)
data$label <- as.character(data$time)

leaflet() %>%
  addTiles() %>%
  addPlayback(
    data = data, label = ~label,
    popup = ~ sprintf(
      "I am a popup for <b>%s</b> and <b>%s</b>",
      Name, label
    ),
    popupOptions = popupOptions(offset = c(0, -35)),
    options = playbackOptions(
      radius = 3,
      tickLen = 36000,
      speed = 50,
      maxInterpolationTime = 1000
    ),
    pathOpts = pathOptions(weight = 5)
  )


## Multiple Elements
data <- sf::st_as_sf(leaflet::atlStorms2005[1:5, ])
data$Name <- as.character(data$Name)
data <- st_cast(data, "POINT")
data$time <- unlist(lapply(rle(data$Name)$lengths, function(x) {
  seq.POSIXt(as.POSIXct(Sys.Date() - 2), as.POSIXct(Sys.Date()), length.out = x)
}))
data$time <- as.POSIXct(data$time, origin = "1970-01-01")
data$label <- paste0("Time: ", data$time)
data$popup <- sprintf(
  "<h3>Customized Popup</h3><b>Name</b>: %s<br><b>Time</b>: %s",
  data$Name, data$time
)
data <- split(data, f = data$Name)

leaflet() %>%
  addTiles() %>%
  addPlayback(
    data = data,
    popup = ~popup,
    label = ~label,
    popupOptions = popupOptions(offset = c(0, -35)),
    labelOptions = labelOptions(noHide = TRUE),
    options = playbackOptions(
      radius = 3,
      tickLen = 1000000,
      speed = 5000,
      maxInterpolationTime = 10000,
      transitionpopup = FALSE,
      transitionlabel = FALSE,
      playCommand = "Let's go",
      stopCommand = "Stop it!",
      color = c(
        "red", "green", "blue",
        "orange", "yellow"
      )
    ),
    pathOpts = pathOptions(weight = 5)
  )

leaflet.extras2 documentation built on April 3, 2025, 10:46 p.m.