-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_WeightResults.R
175 lines (163 loc) · 4.55 KB
/
class_WeightResults.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
173
174
175
#' @include internal.R class_Variable.R
NULL
#' Weight results class
#'
#' Definition for the `WeightResults` class.
#'
#' @seealso [new_weight_results()].
#'
#' @export
WeightResults <- R6::R6Class(
"WeightResults",
public = list(
#' @field id `character` value.
id = NA_character_,
#' @field weight [Weight] object.
weight = NULL,
#' @field status `logical` value.
status = NA,
#' @field factor `numeric` value.
factor = NA_real_,
#' @field current `numeric` value.
current = NA_real_,
#' @field held `numeric` value.
held = NA_real_,
#' @description
#' Create a new `WeightResults` object.
#' @param id `character` value.
#' @param weight [Weight] object.
#' @param held `numeric` value.
#' @return A new `WeightResults` object.
## constructor
initialize = function(id, weight, held) {
### assert that arguments are valid
assertthat::assert_that(
#### id
assertthat::is.string(id),
assertthat::noNA(id),
### weight
inherits(weight, "Weight"),
#### held
assertthat::is.number(held),
assertthat::noNA(held)
)
### set fields
self$id <- id
self$weight <- weight
self$status <- weight$status
self$factor <- weight$factor
self$held <- held
self$current <- weight$current
},
#' @description
#' Print the object.
#' @param ... not used.
print = function(...) {
message("WeightResults")
message(" id: ", self$id)
message(" status: ", self$status)
message(" factor: ", round(self$factor, 2))
message(" current: ", round(self$current, 2))
message(" held: ", round(self$held, 2))
message(" weight: ", self$weight$repr())
invisible(self)
},
#' @description
#' Generate a `character` summarizing the representation of the object.
#' @param start `character` symbol used to start the setting list.
#' Defaults to `"["`.
#' @param end `character` symbol used to start the setting list.
#' Defaults to `"]"`.
#' @return `character` value.
repr = function(start = "[", end = "]") {
paste0(
self$weight$name,
" ", start, "status: ", self$status,
", factor: ", round(self$factor, 2),
", held: ", round(self$held, 2), end, nl(),
" weight: ", self$weight$repr()
)
},
#' @description
#' Get results.
#' @return [tibble::tibble()] object.
get_results_data = function() {
tibble::tibble(
name = self$weight$name,
status = self$status,
total = self$weight$variable$total,
current = self$current,
factor = self$factor,
held = self$held,
units = self$weight$variable$units
)
},
#' @description
#' Get data for displaying the object in a [solutionResults()] widget.
#' @return `list` with widget data.
get_widget_data = function() {
list(
id = self$id,
name = self$weight$name,
status = self$status,
factor = self$factor,
total_amount = self$weight$variable$total,
current_held = self$current,
solution_held = self$held,
units = self$weight$variable$units,
provenance = self$weight$variable$provenance$get_widget_data(),
type = "weight_results"
)
}
)
)
#' New weight results
#'
#' Create a new [WeightResults] object to store results for a solution.
#'
#' @param weight [Weight] object.
#'
#' @param held `numeric` proportion of the weight covered by the solution.
#' (e.g. 0.1 = 10%).
#'
#' @inheritParams new_weight
#'
#' @return A [WeightResults] object.
#'
#' @examples
#' # find data file paths
#' f1 <- system.file(
#' "extdata", "projects", "sim_raster", "sim_raster_spatial.tif",
#' package = "wheretowork"
#' )
#' f2 <- system.file(
#' "extdata", "projects", "sim_raster", "sim_raster_attribute.csv.gz",
#' package = "wheretowork"
#' )
#' f3 <- system.file(
#' "extdata", "projects", "sim_raster", "sim_raster_boundary.csv.gz",
#' package = "wheretowork"
#' )
#'
#' # create new dataset
#' d <- new_dataset(f1, f2, f3)
#'
#' # create new variable
#' v <- new_variable_from_auto(d, index = 1)
#'
#' # create a new weight
#' w <- new_weight(name = "NDVI", variable = v)
#'
#' # create a new weight results object to store results
#' wr <- new_weight_results(w, 80)
#'
#' # print object
#' print(w)
#' @export
new_weight_results <- function(weight, held, id = uuid::UUIDgenerate()) {
WeightResults$new(
id = id,
weight = weight,
held = held
)
}