File: interpolate_along.R

package info (click to toggle)
r-cran-tweenr 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,060 kB
  • sloc: cpp: 1,052; ansic: 558; sh: 13; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
interpolate_numeric_along <- function(data, group, frame, frames, ease, history, keep_last) {
  numeric_along_interpolator(as.numeric(data), as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames), as.character(ease))
}

interpolate_logical_along <- function(data, group, frame, frames, ease, history, keep_last) {
  res <- interpolate_numeric_along(data, group, frame, frames, ease, history, keep_last)
  res[['data']] <- as.logical(round(res[['data']]))
  res
}

#' @importFrom farver decode_colour encode_colour
interpolate_colour_along <- function(data, group, frame, frames, ease, history, keep_last) {
  data <- decode_colour(data, alpha = TRUE, to = 'lab')
  col <- colour_along_interpolator(data, as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames), as.character(ease))
  data.frame(
    data = encode_colour(col[, 1:3, drop = FALSE], alpha = col[,4], from = 'lab'),
    group = col$group,
    frame = col$frame,
    stringsAsFactors = FALSE
  )
}

interpolate_constant_along <- function(data, group, frame, frames, ease, history, keep_last) {
  constant_along_interpolator(as.character(data), as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames), as.character(ease))
}

interpolate_character_along <- interpolate_constant_along

interpolate_date_along <- function(data, group, frame, frames, ease, history, keep_last) {
  res <- interpolate_numeric_along(data, group, frame, frames, ease, history, keep_last)
  res[['data']] <- as.Date(res[['data']], origin = BASEDATE)
  res
}
interpolate_datetime_along <- function(data, group, frame, frames, ease, history, keep_last) {
  if (inherits(data, 'POSIXlt')) {
    warning("POSIXlt converted to POSIXct")
    data <- as.POSIXct(data)
  }
  tz <- attr(data, 'tzone')
  res <- interpolate_numeric_along(data, group, frame, frames, ease, history, keep_last)
  res[['data']] <-  as.POSIXct(res[['data']], origin = BASEDATETIME, tz = tz)
  res
}
interpolate_factor_along <- function(data, group, frame, frames, ease, history, keep_last) {
  all_levels <- levels(data)
  ord <- is.ordered(data)
  res <- interpolate_character_along(data, group, frame, frames, ease, history, keep_last)
  res[['data']] <- if (ord) ordered(res[['data']], all_levels) else factor(res[['data']], all_levels)
  res
}
interpolate_list_along <- function(data, group, frame, frames, ease, history, keep_last) {
  new_data <- list_along_interpolator(as.list(data), as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames), as.character(ease))
  attributes(new_data$data) <- attributes(data)
  new_data
}
interpolate_numlist_along <- function(data, group, frame, frames, ease, history, keep_last) {
  new_data <- numlist_along_interpolator(lapply(data, as.numeric), as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames), as.character(ease))
  attributes(new_data$data) <- attributes(data)
  new_data
}
get_phase_along <- function(group, frame, frames, history, keep_last) {
  phase_along_interpolator(as.integer(group), as.numeric(frame), as.logical(history), as.logical(keep_last), as.numeric(frames))
}