forked from uptake/updraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_custom_function.R
217 lines (194 loc) · 7.36 KB
/
module_custom_function.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#' @title Custom Function Module
#' @name CustomFunctionModule
#' @description Implementation of \code{ModuleInterface} for executing a user defined function. Uses
#' \code{future} package for execution.
#' @importFrom future resolved
#' @importFrom jsonlite serializeJSON
#' @importFrom R6 R6Class
#' @section Class Constructor:
#' \describe{
#' \item{\code{new(name, fun)}}{
#' \itemize{
#' \item{\code{name}: Unique character string that identifies this module instance.}
#' \item{\code{fun}: function this module will execute. Must be a reference to the actual function.}
#' }
#' }
#' }
#' @section Public:
#' \describe{
#' \item{\code{clearOutputCache()}}{
#' \itemize{
#' \item{Clears output cache stored in this instance so memory can be reclaimed.}
#' \item{\bold{Returns}: \code{NULL}.}
#' }
#' }
#'
#' \item{\code{errorCheck(executionCheck = FALSE, ...)}}{
#' \itemize{
#' \item{Runs error checking on the internal state of this object for erroneous values.}
#' \item{\code{executionCheck}: when set to \code{TRUE}, runs additional checks to determine if ready for execution.}
#' \item{\code{...}: not used by this component.}
#' \item{\bold{Returns}: \code{NULL}, will raise a fatal error if an error is found.}
#' }
#' }
#'
#' \item{\code{getFuncObj()}}{
#' \itemize{
#' \item{Gets the base R function object that is called when a module's execution is started.}
#' \item{\bold{Returns}: R function object.}
#' }
#' }
#'
#' \item{\code{getInputs()}}{
#' \itemize{
#' \item{Gets a named logical vector where the names are the possible inputs into a module and the values indicate if an input is required.}
#' \item{\bold{Returns}: logical vector.}
#' }
#' }
#'
#' \item{\code{getName()}}{
#' \itemize{
#' \item{Gets the name of this function.}
#' \item{\bold{Returns}: character string that is the name of this module.}
#' }
#' }
#'
#' \item{\code{getOutput()}}{
#' \itemize{
#' \item{Gets the output of the function executed by this module.}
#' \item{\bold{Returns}: The output of the function if the execution has completed. \code{NULL} will be returned if the execution has not been started or is still in progress.}
#' }
#' }
#'
#' \item{\code{getSaveInfo()}}{
#' \itemize{
#' \item{Gets a named list of internal states of this object which can be used to save this object on disk.}
#' \item{\bold{Returns}: a named list}
#' }
#' }
#'
#' \item{\code{hasCompleted()}}{
#' \itemize{
#' \item{Indicates if the execution of the associated function of this module is complete.}
#' \item{\bold{Returns}: a boolean where \code{TRUE} indicates the execution is complete and \code{FALSE} indicates the execution is not complete or not started.}
#' }
#' }
#'
#' \item{\code{startExecution(args)}}{
#' \itemize{
#' \item{Starts the execution of the function associated with this module. This is non-blocking if using an asynchronous.}
#' \item{\bold{\code{args}}: named list of arguments for function associated with this module}
#' \item{\bold{Returns}: \code{NULL}}
#' }
#' }
#' }
#' @section Private:
#' \describe{
#' \item{\code{fun}}{
#' \itemize{
#' \item{Stores associated function name.}
#' }
#' }
#'
#' \item{\code{futurePromise}}{
#' \itemize{
#' \item{Stores promise of future output of a function.}
#' }
#' }
#'
#' \item{\code{name}}{
#' \itemize{
#' \item{Unique character string that identifies this module instance.}
#' }
#' }
#' }
#' @section Static Class Methods:
#' \describe{
#' \item{\link[updraft]{CustomFunctionModule-cash-initFromSaveData}}{}
#' }
#' @export
CustomFunctionModule <- R6::R6Class("CustomFunctionModule"
, inherit = ModuleInterface
, public = list(
initialize = function(name
, fun
) {
private$name <- name
private$fun <- fun
return(self$errorCheck())
}
, clearOutputCache = function() {
# TODO: this rm is causing issues
# if (!is.null(private$futurePromise)) {
# rm(private$futurePromise)
# }
private$futurePromise <- NULL
return(invisible(self))
}
, errorCheck = function(executionCheck = FALSE
, ...
) {
if (!is.character(private$name) && length(private$name) == 1) {
UpDraftSettings$errorLogger("name value must be a single character string")
}
if (private$name == '') {
UpDraftSettings$errorLogger("name value cannot be an empty string")
}
if (!is.function(private$fun)) {
UpDraftSettings$errorLogger("fun must be a reference to a function")
}
return(invisible(self))
}
, getFuncObj = function() {
#TODO: unit tests
return(private$fun)
}
, getName = function() {
return(private$name)
}
, getInputs = function() {
return(GetFunctionArgs(private$fun))
}
, getOutput = function() {
if (self$hasCompleted()) {
return(future::value(private$futurePromise))
}
return(NULL)
}
, getSaveInfo = function() {
internalState = list()
internalState[["class"]] <- class(self)[1]
internalState[['name']] <- private$name
internalState[['fun']] <- jsonlite::serializeJSON(private$fun)
return(internalState)
}
, hasCompleted = function() {
if (is.null(private$futurePromise) || !future::resolved(private$futurePromise)) {
return(FALSE)
}
return(TRUE)
}
, startExecution = function(args) {
private$futurePromise <- FutureFunctionCall(func = private$fun
, args = args
, funcName = self$getName())
return(invisible(NULL))
}
)
, private = list(
fun = NULL
, futurePromise = NULL
, name = ""
)
)
#' @title CustomFunctionModule Init From Save Data
#' @name CustomFunctionModule$initFromSaveData
#' @description Static factory method that generates a CustomFunctionModule
#' from json save data.
#' @param saveData json saved data generated through the method \code{getSaveInfo}.
#' @importFrom jsonlite unserializeJSON
CustomFunctionModule$initFromSaveData <- function(saveData) {
module <- CustomFunctionModule$new(name = saveData$name
, fun = jsonlite::unserializeJSON(saveData$fun))
return(module)
}