-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.R
56 lines (48 loc) · 1.36 KB
/
range.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
# initialise a continuous range
continuous_range <- function(limits = NULL) {
rng <- scales::ContinuousRange$new()
rng$train(limits)
rng
}
# initialise a discrete range
discrete_range <- function(levels = NULL) {
rng <- DiscreteRange$new()
rng$train(levels)
rng
}
# re-implementation of scales::DiscreteRange, with the following changes:
# - input order preserved for non-factor input
# - doesn't coerce logical as string
DiscreteRange <- R6::R6Class(
"DiscreteRange",
inherit = scales::Range,
list(
train = function(x) {
if (is.null(x)) return()
tidyassert::assert(is_discrete(x), "Continuous value supplied to discrete scale")
lvls <- if (is.factor(x)) levels(x) else unique(x)
lvls <- lvls[!is.na(lvls)]
self$range <- unique(c(self$range, lvls))
},
reset = function() self$range <- NULL
)
)
# initialise a continuous identity range
continuous_identity_range <- function(x = NULL) {
rng <- ContinuousIdentityRange$new()
rng$train(x)
rng
}
# mutable continuous identity range
ContinuousIdentityRange <- R6::R6Class(
"ContinuousIdentityRange",
inherit = scales::Range,
list(
train = function(x) {
if (is.null(x)) return()
tidyassert::assert(is.numeric(x), "Discrete data supplied to continuous scale")
self$range <- c(self$range, x)
},
reset = function() self$range <- NULL
)
)