-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_ManualLegend.R
111 lines (101 loc) · 2.69 KB
/
class_ManualLegend.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
#' @include internal.R
NULL
#' Manual legend class
#'
#' Definition for the `ManualLegend` class.
ManualLegend <- R6::R6Class(
"ManualLegend",
public = list(
#' @field values `values` vector.
values = NA_real_,
#' @field colors `character` vector.
colors = NA_character_,
#' @field labels `character` vector.
labels = NA_character_,
#' @description
#' Create a `ManualLegend` object.
#' @param values `numeric` vector of values.
#' @param colors `character` vector of colors.
#' @param labels `character` vector of labels.
#' @return A new `ManualLegend` object.
initialize = function(values, colors, labels) {
assertthat::assert_that(
length(colors) == length(labels),
is.numeric(values),
assertthat::noNA(values),
length(values) == length(labels),
is.character(colors),
assertthat::noNA(colors),
all(nchar(colors) %in% c(7, 9)),
all(substr(colors, 1, 1) == "#"),
is.character(labels),
assertthat::noNA(labels)
)
self$values <- values
self$colors <- colors
self$labels <- labels
},
#' @description
#' Get resample method.
#' @return `character` object.
get_resample_method = function() {
"ngb"
},
#' @description
#' Get a function for mapping values to colors.
#' @return A `function` object.
get_color_map = function() {
leaflet::colorFactor(
palette = self$colors,
domain = NULL,
levels = self$values,
alpha = TRUE,
na.color = NA
)
},
#' @description
#' Get data for creating a widget.
#' @return A `list` object.
get_widget_data = function() {
list(
values = self$labels,
colors = self$colors,
type = "ManualLegend"
)
},
#' @description
#' Export settings
#' @return `list` object.
export = function() {
list(
type = "manual",
colors = self$colors,
labels = self$labels
)
}
)
)
#' New manual legend
#'
#' Create a new [ManualLegend] object.
#'
#' @param values `numeric` Values that are linked to the labels.
#'
#' @param colors `character` Colors to show in the legend.
#' These colors should be in hex format (e.g. `"#AABBCC"`).
#' Arguments should contain two different colors.
#'
#' @param labels `character` Labels to show in the legend.
#'
#' @return A [ManualLegend] object.
#'
#' @examples
#' # create new object
#' l <- new_manual_legend(c(0, 1), c("#000000", "#AAAAAA"), c("a", "b"))
#'
#' # print object
#' print(l)
#' @export
new_manual_legend <- function(values, colors, labels) {
ManualLegend$new(values, colors, labels)
}