-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmethods.R
160 lines (146 loc) · 4.87 KB
/
methods.R
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#' Print moveVis frames
#'
#' Method for printing \code{moveVis} frames. Prints show basic information about the object, including number of frames, extent and more.
#'
#' @param x an object of class \code{moveVis}.
#' @param ... further arguments passed to or from other methods.
#'
#' @return
#' Invisible, used for its side effect.
#'
#' @rdname print
#' @export
print.moveVis <- function(x, ...) {
if(inherits(x, "frames_spatial")){
cat(paste0("Spatial frames of class moveVis\n"))
cat(paste0("number of frames: ", as.character(length(x)), "\n"))
cat(paste0("temporal extent: ", paste0(x$move_data$time_chr[1], "' to '", x$move_data$time_chr[nrow(x$move_data)]), "\n"))
cat(paste0("spatial extent: ", paste0(mapply(x = names(x$aesthetics$gg.ext), y = x$aesthetics$gg.ext, function(x, y) paste0(x, ": ", round(y, digits = 5)), USE.NAMES = F), collapse = "; "), "\n"))
cat(paste0("raster type: ", x$aesthetics$r_type, "\n"))
cat(paste0("basemap: ", if(x$aesthetics$map_service != "custom") paste0("'", x$aesthetics$map_type, "' from '", x$aesthetics$map_service, "'") else "custom", "\n"))
cat(paste0("names: '", paste0(unique(x$move_data$name), collapse = "', '"), "'\n"))
#cat(paste0("added function: ", length(x$additions), "\n"))
}
if(inherits(x, "frames_graph")){
cat(paste0("Graph frames of class moveVis\n"))
cat(paste0("number of frames: ", as.character(length(x)), "\n"))
cat(paste0("temporal extent: ", paste0(x$move_data$time_chr[1], "' to '", x$move_data$time_chr[nrow(x$move_data)]), "\n"))
cat(paste0("raster type: ", x$aesthetics$r_type, "\n"))
cat(paste0("names: '", paste0(unique(x$move_data$name), collapse = "', '"), "'\n"))
#cat(paste0("added function: ", length(frames$additions), "\n"))
}
if(inherits(x, "frames_joined")){
cat(paste0("Joined frames of class moveVis\n"))
cat(paste0("number of frames: ", as.character(length(x)), "\n"))
cat(paste0("temporal extent: -"))
cat(paste0("raster type: -"))
cat(paste0("names: '", paste0(unique(x$move_data$name), collapse = "', '"), "'\n"))
}
}
#' Length of moveVis frames
#'
#' Method to get length of \code{moveVis} frames, i.e. number of frames.
#'
#' @inheritParams print.moveVis
#'
#' @return
#' Numeric
#'
#' @rdname length
#' @export
length.moveVis <- function(x){
if(inherits(x, "frames_joined")){
length(x$frames_lists[[1]])
}else{
length(unique(x$move_data$frame))
}
}
#' Combining moveVis frames
#'
#' Method for combining multiple \code{moveVis} frames objects.
#'
#' @param ... two or more objects of class \code{moveVis}.
#'
#' @return
#' A list of \code{moveVis} frames objects.
#'
#' @rdname c
#' @export
c.moveVis <- function(...){
frames <- list(...)
return(frames)
}
# head method
#' @rdname head
#' @importFrom utils tail
#' @export
tail.moveVis <- function(x, n = 6L, ...){
x[utils::tail(1:length(x), n, ...)]
}
#' Return first or last frames of an moveVis frames object
#'
#' Method for returning \code{n} last or first frames of a \code{moveVis} frames objects.
#'
#' @inheritParams print.moveVis
#' @param n an integer of length up to \code{length(x)}.
#'
#' @return
#' A \code{moveVis} frames object.
#'
#' @rdname head
#' @importFrom utils head
#' @export
head.moveVis <- function(x, n = 6L, ...){
x[utils::head(1:length(x), n, ...)]
}
#' Reverse moveVis frames
#'
#' Method for reversing the order of frames in a \code{moveVis} frames object.
#'
#' @inheritParams print.moveVis
#'
#' @return
#' A \code{moveVis} frames object.
#'
#' @rdname rev
#' @export
rev.moveVis <- function(x){
x[rev(1:length(x))]
}
#' Extract moveVis frames
#'
#' Method for extracting individual frames or a sequence of frames from a \code{moveVis} frames object.
#'
#' @inheritParams print.moveVis
#' @param i numeric, index number or sequence of index numbers of the frame(s) to be extracted.
#'
#' @return
#' A \code{moveVis} frames object.
#'
#' @rdname Extract
#' @export
"[.moveVis" <- function(x, i, ...) {
bounds <- sapply(i, function(j) any(j < 1, j > length(x)))
if(all(bounds)) stop(paste0("Subscript out of bounds. Length of frames is ", length(x), "."), call. = FALSE)
if(any(bounds)) warning(paste0("Subscript extends beyond bounds and is thus truncated. Length of frames is ", length(x), "."), call. = FALSE, immediate. = FALSE)
i <- i[!bounds]
# seubsetting
.sub <- function(x, i){
sub <- apply(sapply(i, function(j) x$move_data$frame == j), MARGIN = 1, any)
x$move_data <- x$move_data[sub,]
if(length(x$raster_data) > 1) x$raster_data <- x$raster_data[sub]
return(x)
}
if(inherits(x, "frames_joined")){
x$frames_lists <- lapply(x$frames_lists, function(x) x[i])
}else{
x <- .sub(x, i)
}
return(x)
}
# render methods
#' @rdname render_frame
#' @export
"[[.moveVis" <- function(x, i, ...) {
quiet(render_frame(x, i))
}