-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_Provenance.R
169 lines (156 loc) · 3.91 KB
/
class_Provenance.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
#' @include internal.R
NULL
#' Provenance class
#'
#' Definition for the Provenance class.
#'
#' @seealso [new_provenance()].
Provenance <- R6::R6Class(
"Provenance",
public = list(
#' @field name `character` value.
name = NA_character_,
#' @field icon `character` value
icon = NA_character_,
#' @field color `character` value.
color = NA_character_,
#' @field description `character` value
description = NA_character_,
#' @description
#' Create a Provenance object.
#' @param name `character` value.
#' @param icon `numeric` value
#' @param color `character` value.
#' @param description `numeric` value.
#' @return A Provenance object.
initialize = function(name, icon, color, description) {
assertthat::assert_that(
assertthat::is.string(name),
assertthat::noNA(name),
assertthat::is.string(icon),
assertthat::noNA(icon),
assertthat::is.string(color),
assertthat::noNA(color),
assertthat::is.string(description),
assertthat::noNA(description)
)
self$name <- name
self$icon <- icon
self$color <- color
self$description <- description
},
#' @description
#' Print the object.
#' @param ... not used.
print = function(...) {
message(self$repr())
invisible(self)
},
#' @description
#' Generate a `character` summarizing the representation of the object.
#' @param ... not used.
#' @return `character` value.
repr = function(...) {
self$name
},
#' @description
#' Get widget data.
#' @return `list` object.
get_widget_data = function() {
list(
name = self$name,
icon = self$icon,
color = self$color,
description = self$description
)
},
#' @description
#' Get widget data.
#' @return `character` value.
export = function() {
self$name
}
)
)
#' New Provenance
#'
#' Create a new [Provenance] object.
#'
#' @param name `character` name of provenance source.
#'
#' @param icon `character` name of icon to depict the soruce.
#'
#' @param color `character` value containing the color for the icon.
#'
#' @param description `character` value containing a description of the
#' provenance.
#'
#' @return A [Provenance] object.
#'
#' @examples
#' # create a provenance
#' x <- new_provenance(
#' name = "Global", icon = "globe", color = "#123456",
#' description = "Data is global."
#' )
#'
#' # print object
#' print(x)
#' @export
new_provenance <- function(name, icon, color, description) {
Provenance$new(
name = name,
icon = icon,
color = color,
description = description
)
}
#' New provenance from source
#'
#' Create a new [Provenance] object from a recognized source.
#'
#' @param source `character` provenance type.
#' Available options include `"national"`, `"regional"`, or `"missing"`.
#'
#' @inherit new_provenance return
#'
#' @examples
#' # create a provenance
#' x <- new_provenance_from_source("missing")
#'
#' # print object
#' print(x)
#'
#' @export
new_provenance_from_source <- function(source) {
# assert argument is valid
assertthat::assert_that(
assertthat::is.string(source),
assertthat::noNA(source))
assertthat::assert_that(source %in% c("national", "regional", "missing"))
# create object
if (identical(source, "national")) {
out <- new_provenance(
name = source,
icon = "canadian-maple-leaf",
color = "#EF3340",
description = "This contains National data"
)
} else if (identical(source, "regional")) {
out <- new_provenance(
name = source,
icon = "certificate",
color = "#43a2ca",
description = "This contains Regional data"
)
} else {
out <- new_provenance(
name = source,
icon = "question",
color = "#7F7F7F",
description = "This contains data of unknown provenance"
)
}
# return result
out
}