/
s3-array.R
62 lines (55 loc) · 1.94 KB
/
s3-array.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
constructors$array <- new.env()
#' Constructive options for arrays
#'
#' These options will be used on arrays. Note that arrays can be built on top of
#' vectors, lists or expressions. Canonical arrays have an implicit class "array"
#' shown by `class()` but "array" is not part of the class attribute.
#'
#' Depending on `constructor`, we construct the object as follows:
#' * `"array"` (default): Use the `array()` function
#' * `"next"` : Use the constructor for the next supported class. Call `.class2()`
#' on the object to see in which order the methods will be tried.
#'
#' @param constructor String. Name of the function used to construct the object, see Details section.
#' @inheritParams opts_atomic
#' @return An object of class <constructive_options/constructive_options_array>
#' @export
opts_array <- function(constructor = c("array", "next"), ...) {
.cstr_combine_errors(
constructor <- .cstr_match_constructor(constructor, "array"),
check_dots_empty()
)
.cstr_options("array", constructor = constructor)
}
#' @export
.cstr_construct.array <- function(x, ...) {
opts <- .cstr_fetch_opts("array", ...)
if (is_corrupted_array(x) || opts$constructor == "next") return(NextMethod())
constructor <- constructors$array[[opts$constructor]]
constructor(x, ...)
}
is_corrupted_array <- function(x) {
!("dim" %in% names(attributes(x)) && (is.atomic(x) || is.list(x) || is.expression(x)))
}
constructors$array$array <- function(x, ...) {
# build args for array() call
x_stripped <- x
attributes(x_stripped) <- NULL
args <- list(
x_stripped,
dim = attr(x, "dim")
)
dimnames <- attr(x, "dimnames")
args$dim_names <- if (!is.null(dimnames)) list(dimnames = dimnames)
# build code
code <- .cstr_apply(args, fun = "array", ...)
# repair
repair_attributes_array(x, code, ...)
}
repair_attributes_array <- function(x, code, ..., pipe = NULL) {
.cstr_repair_attributes(
x, code, ...,
pipe = pipe,
ignore = "dim"
)
}