-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_IncludeResults.R
160 lines (150 loc) · 4.14 KB
/
class_IncludeResults.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
#' @include internal.R class_Variable.R
NULL
#' Include results class
#'
#' Definition for the `IncludeResults` class.
#'
#' @seealso [new_include_results()].
#'
#' @export
IncludeResults <- R6::R6Class(
"IncludeResults",
public = list(
#' @field id `character` value.
id = NA_character_,
#' @field include [Include] object.
include = NA_character_,
#' @field status `logical` value.
status = NA,
#' @field held `numeric` value.
held = NA_real_,
#' @description
#' Create a new `IncludeResults` object.
#' @param id `character` value.
#' @param include [Include] object.
#' @param held `numeric` value.
#' @return A new `IncludeResults` object.
## constructor
initialize = function(id, include, held) {
### assert that arguments are valid
assertthat::assert_that(
#### id
assertthat::is.string(id),
assertthat::noNA(id),
### include
inherits(include, "Include"),
#### held
assertthat::is.number(held),
assertthat::noNA(held)
)
### set fields
self$id <- id
self$include <- include
self$status <- include$status
self$held <- held
},
#' @description
#' Print the object.
#' @param ... not used.
print = function(...) {
message("IncludeResults")
message(" id: ", self$id)
message(" status: ", self$status)
message(" held: ", round(self$held, 2))
message(" include: ", self$include$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$include$name,
" ", start, "status: ", self$status,
", held: ", round(self$held, 2), end, nl(),
" include: ", self$include$repr()
)
},
#' @description
#' Get results.
#' @return [tibble::tibble()] object.
get_results_data = function() {
tibble::tibble(
name = self$include$name,
status = self$status,
total = self$include$variable$total,
held = self$held,
units = self$include$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$include$name,
status = self$status,
total_amount = self$include$variable$total,
solution_held = self$held,
units = self$include$variable$units,
provenance = self$include$variable$provenance$get_widget_data(),
type = "include_results"
)
}
)
)
#' New include results
#'
#' Create a new [IncludeResults] object to store results for a solution.
#'
#' @param include [Weight] object.
#'
#' @param held `numeric` proportion of the include covered by the solution.
#' (e.g. 0.1 = 10%).
#'
#' @inheritParams new_include
#'
#' @return A [IncludeResults] 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 include
#' i <- new_include(name = "NDVI", variable = v)
#'
#' # create a new include results object to store results
#' ir <- new_include_results(i, 80)
#'
#' # print object
#' print(ir)
#' @export
new_include_results <- function(include, held, id = uuid::UUIDgenerate()) {
IncludeResults$new(
id = id,
include = include,
held = held
)
}