-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.r
172 lines (145 loc) · 5.11 KB
/
utils.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
161
162
163
164
165
166
167
168
169
170
171
172
#' @importFrom utils getFromNamespace
#' @importFrom magrittr %>%
#' @export
magrittr::`%>%`
as_tbl_ord_default <- function(x) {
class(x) <- c("tbl_ord", class(x))
x
}
tbl_ord_factors <- c(
rows = "rows", columns = "cols", cols = "cols", dims = "dims",
f = "rows", g = "cols", fg = "dims",
u = "rows", v = "cols", uv = "dims",
left = "rows", right = "cols",
# cases = "rows", variables = "cols",
# subjects = "rows", measures = "cols",
# scores = "rows", loadings = "cols",
rowprincipal = "rows", colprincipal = "cols", columnprincipal = "cols",
both = "dims", symmetric = "dims"
)
match_factor <- function(x) {
x <- match.arg(tolower(x), names(tbl_ord_factors))
unname(tbl_ord_factors[x])
}
switch_inertia <- function(x) {
x <- match.arg(tolower(x), names(tbl_ord_factors))
switch(tbl_ord_factors[x], rows = c(1, 0), cols = c(0, 1), dims = c(.5, .5))
}
method_classes <- function(generic.function) {
stringr::str_replace(
stringr::str_subset(
rownames(attr(utils::methods(generic.function), "info")),
paste0("^", generic.function, "\\.")
),
paste0("^", generic.function, "\\."), ""
)
}
factor_coord <- function(x) {
if (any(duplicated(x))) stop("Duplicated coordinates detected.")
factor(x, levels = x)
}
# `ggbiplot` utilities
matrix_stat <- function(.matrix, stat) {
.matrix <- match_factor(.matrix)
if (stat == "identity") return(.matrix)
stringr::str_c(.matrix, stat, sep = "_")
}
rows_stat <- function(stat) matrix_stat("rows", stat)
cols_stat <- function(stat) matrix_stat("cols", stat)
# get ordination coordinate mapping from a data frame in a stat layer:
# `ord_aes()` if specified, otherwise 'x' and 'y'
get_ord_aes <- function(data) {
ord_cols <- grep("^\\.\\.coord[0-9]+$", names(data))
if (length(ord_cols) == 0) ord_cols <- match(c("x", "y"), names(data))
ord_cols
}
# restrict to specified elements
setup_elts_data <- function(data, params) {
# if specified and possible, restrict to active or supplementary elements
if (! is.null(params$elements) && ".element" %in% names(data)) {
# ensure that `elements` is a character singleton
stopifnot(
is.character(params$elements),
length(params$elements) == 1L
)
# subset accordingly
data <- if ("all" %in% params$elements) {
data
} else {
data[data$.element == params$elements, , drop = FALSE]
}
# print note if both `elements` and `subset` are passed
if (! is.null(params$subset)) {
message("`subset` will be applied after data are restricted to ",
params$elements, " elements.")
}
}
# by default, render elements for all rows
if (! is.null(params$subset)) {
if (is.numeric(params$subset)) {
data <- data[params$subset, , drop = FALSE]
} else if (is.character(params$subset)) {
warning("`subset` cannot yet accept names.")
} else {
warning("`subset` of unrecognized type will be ignored.")
}
}
data
}
# restrict to a matrix factor
setup_rows_data <- function(data, params) {
data <-
data[data$.matrix == "rows", -match(".matrix", names(data)), drop = FALSE]
setup_elts_data(data, params)
}
setup_cols_data <- function(data, params) {
data <-
data[data$.matrix == "cols", -match(".matrix", names(data)), drop = FALSE]
setup_elts_data(data, params)
}
# restrict to a matrix factor and to the first two coordinates
# (for stat layers that only accept 'x' and 'y')
setup_rows_xy_data <- function(data, params) {
data <- setup_rows_data(data, params)
ord_cols <- get_ord_aes(data)
# if necessary, restore 'x' and 'y' from first and second coordinates
if (any(is.na(match(c("x", "y"), names(data)[ord_cols])))) {
xy_cols <- match(c("..coord1", "..coord2"), names(data)[ord_cols])
names(data)[xy_cols] <- c("x", "y")
}
data
}
setup_cols_xy_data <- function(data, params) {
data <- setup_cols_data(data, params)
ord_cols <- get_ord_aes(data)
# if necessary, restore 'x' and 'y' from first and second coordinates
if (any(is.na(match(c("x", "y"), names(data)[ord_cols])))) {
xy_cols <- match(c("..coord1", "..coord2"), names(data)[ord_cols])
names(data)[xy_cols] <- c("x", "y")
}
data
}
setup_referent_params <- function(self, data, params) {
.matrix <- tolower(gsub("^Stat(Rows|Cols).*$", "\\1", class(self)[[1L]]))
stopifnot(.matrix %in% c("rows", "cols"))
setup_factor_xy_data <- switch(
.matrix,
rows = setup_cols_xy_data,
cols = setup_rows_xy_data
)
# syntactic sugar to specify the reference data
if (is.null(params$referent)) {
# default null `referent` to other matrix factor
params$referent <- setup_factor_xy_data(data, list())
} else if (! is.data.frame(params$referent) && ! is.matrix(params$referent)) {
# if `referent` is not a data frame, then treat it as the `subset` param
params$referent <-
setup_factor_xy_data(data, list(subset = params$referent))
} else {
# continue with parent parameter setup
params$referent <- as.data.frame(params$referent)
params <- ggproto_parent(StatReferent, self)$setup_params(data, params)
}
params
}
is_const <- function(x) length(unique(x)) == 1L