-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs2.R
More file actions
executable file
·351 lines (252 loc) · 10.7 KB
/
funcs2.R
File metadata and controls
executable file
·351 lines (252 loc) · 10.7 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
as.c = as.character
options(stringsAsFactors = FALSE)
# ---------------------------------------- get job names ------------------------------------------------------------
#' get job names
#'
#' by default, one may remove a specific index. So that this works well for previous jobs
#'
#' @params x is the input
#' @params drop numeric index to drop
get_jobnames <- function(input, drop){
nms <- names(input);
#print(nms)
# get a list of job ids
job_attrs <- sprintf("job-%s_name", 1:input$num_jobs)
#print(job_attrs)
if(!missing(drop))
job_attrs <- job_attrs[-drop] ## remove itself
nms <- lapply(job_attrs, function(y) input[[y]])
names(nms) <- nms
nms <- c("none" = "none", nms)
return(nms)
}
# ---------------------------------------- job forms ------------------------------------------------------------
get_jobforms <- function(input){
w <- lapply(1:input$num_jobs, function(i) {
tmp <- newjobform(input, index=i)
tabPanel(title = tmp$name, wellPanel(tmp$elements))
#w <- c(w, tmp)
})
ret <- do.call(tabsetPanel, w)
# only return th
return(ret)
}
newjobform <- function(input, index){
i = index
#print(input)
# input is a list with various values, stored as a
# get the variables to be used
jobname <- sprintf("job-%d_name",i)
cmd <- sprintf("job-%d_cmd",i)
cpu <- sprintf("job-%d_cpu",i)
prevjob <- sprintf("job-%d_prevjob",i)
subtype <- sprintf("job-%d_subtype",i)
deptype <- sprintf("job-%d_deptype",i)
#print(head(jobid_v, 2))
jobnames <- names(get_jobnames(input, i))
# get values; if dont exist use default
# decide on default values
#dat = try(to_flowdat.reactivevalues(input))
jobid_v <- ifelse(length(input[[jobname]]) > 0, input[[jobname]], sprintf("myjob%s",i) )
cmd_v <- ifelse(length(input[[cmd]]) > 0, input[[cmd]], "sleep 1" )
cpu_v <- ifelse(length(input[[cpu]]) > 0, input[[cpu]], 1 )
# previous jobs is the second last in jobnames
subtype_v <- ifelse(length(input[[subtype]]) > 0, input[[subtype]], 'serial' )
default.prevjob = tail(jobnames, 1)
#print(default.prevjob)
prevjob_v <- ifelse(length(input[[prevjob]]) > 0, input[[prevjob]], default.prevjob)
print(prevjob_v)
# switch deptype, if previous jobs is selected
default.deptype = ifelse(prevjob_v == "none", "none", "serial")
deptype_v <- ifelse(length(input[[deptype]]) > 0, input[[deptype]], default.deptype )
# switch previous job, if dep_type is selected
prevjob_v = ifelse(prevjob_v == "none" & deptype_v != "none", tail(jobnames, 1), prevjob_v)
# if there are rows, where dep is none and prev job is NOT null
deptype_v = ifelse(deptype_v == "none" & prevjob_v != "none", "serial", deptype_v)
ret <- list(
#column(3, wellPanel(
#h3(sprintf("%s", jobid_v)), hr(),
textInput(inputId = jobname, label="Job Name", value = jobid_v),
textInput(inputId = cmd, label="Command", value= cmd_v),
#numericInput(inputId = cpu, label="cpu", value = cpu_v),
selectInput(inputId = cpu, label="cpu", selected = cpu_v, choices = seq(1,64)),
selectInput(inputId = subtype, label="Submission Type",
choices = list(scatter = 'scatter',
serial = 'serial'), selected = subtype_v),
selectInput(inputId = deptype, label="dependency_type",
choices = list(none = "none",
gather = 'gather',
serial = 'serial',
burst = "burst"),
selected = deptype_v)
)
if(length(jobnames) > 0){#print(jobnames);print(prevjob_v)
ret2 =
list(
selectInput(inputId = prevjob,
label = "previous_job",
choices = jobnames,
selected = prevjob_v)
# checkboxGroupInput(inputId = paste0(prevjob, "2"),
# label = 'previous_job2',
# choices = jobnames,
# selected = prevjob_v)
)
ret = c(ret, ret2)
}
return(list(elements=ret, name=jobid_v))
}
# ---------------------------------------- get flowdef ------------------------------------------------------------
# create values from the job form, into a data.frame
to_flowdat.reactivevalues <- function(input){
nms <- names(input)
#cat(nms,"\n\n");
job_attrs <- grep("job-", nms, value=TRUE)
if(length(job_attrs) < 1)
return() ## need a few jobs
job_values <- sapply(job_attrs, function(x){
val = input[[x]]
ifelse(length(val) == 0, NA, paste(val, collapse = ","))
})
print(job_values)
# table of job ID attrs
tmp <- do.call(rbind, strsplit(job_attrs, "_"))
tmp <- data.frame(tmp, job_attrs, job_values)
#print(tmp)
names(tmp) <- c("jobid", "attr", "inputid", "value")
# subset only the ones we need
message("number of jobs: ", input$num_jobs)
# filter tmp
tmp <- subset(tmp, tmp$jobid %in% paste0("job-", 1:input$num_jobs))
message("dim of tmp: ", nrow(tmp))
#print(tmp)
dat <- dcast(tmp, formula = jobid ~ attr, value.var = 'value')
# flowdat = data.frame(
# jobid = dat$jobid,
# attr = dat$attr,
# inputid = dat$inputid,
# value = dat$value)
#
#
# flowdat[1:input$num_jobs, ]
dat
}
to_flowmat.reactivevalues <- function(input){
flowdat <- to_flowdat.reactivevalues(input)
print(flowdat)
# create a flow mat
flowmat <- data.frame(samplename = "samp1",
jobname = flowdat$name,
cmd = flowdat$cmd)
class(flowmat) = c("flowmat", "data.frame")
print(flowmat)
flowmat
}
#' @param input reactive values
to_flowdef.reactivevalues <- function(input){
if (input$num_jobs < 1)
return("")
flowdat <- to_flowdat.reactivevalues(input)
#print(flowdat)
# create a flow mat
flowmat <- to_flowmat.reactivevalues(input)
# from this, get a flowdat
#undebug(flowr:::to_flowdef.flowmat)
flowdef = try(to_flowdef(flowmat,
prev_jobs = flowdat$prevjob,
cpu_reserved = flowdat$cpu,
dep_type = flowdat$deptype,
sub_type = flowdat$subtype))
#print(flowdef)
flowdef
}
# no used
.get_flow_table <- function(input){
nms <- names(input)
#cat(nms,"\n\n");
job_attrs <- grep("job-", nms, value=TRUE)
if(length(job_attrs) < 1) return() ## need a few jobs
job_values <- sapply(job_attrs, function(x) input[[x]])
tmp <- do.call(rbind, strsplit(job_attrs, "_"))
tmp <- data.frame(tmp, job_attrs, job_values)
#print(tmp)
names(tmp) <- c("jobid", "attr", "inputid", "value")
dat <- dcast(tmp, formula = jobid ~ attr, value.var = 'value' )
## prev_jobs dep_type sub_type cpu nodes jobid prev_jobid
jobnames <- dat$name
jobid <- 1:length(jobnames);names(jobid)=jobnames
prev_jobid <- jobid[as.c(dat$prevjob)]
#print(prev_jobid); print(jobid)
flow_dat <- data.frame(jobnames=dat$name, prev_jobs=dat$prevjob,
dep_type=dat$deptype,sub_type=dat$subtype, cpu=dat$cpu,
jobid=jobid, prev_jobid=prev_jobid, cmd = dat$cmd,
stringsAsFactors=FALSE)
return(flow_dat)
}
# ---------------------------------------- get code ------------------------------------------------------------
# generate code from dat
to_flowcode <- function(input,...){
## this would take in a flowdef and produce a code to generate it
#sapply(fobj@jobs, slot, "name")
mat = to_flowmat(input)
def = to_flowdef(input)
dat = dplyr::left_join(def, mat)
jobnames <- def$jobnames;
code_flowdef = code_final = ""
code_initial <- c("# --------------- install flowr ---------------",
'install.packages("flowr", repos = c(CRAN="http://cran.rstudio.com", DRAT="http://sahilseth.github.io/drat"))',
"\n# --------------- load library ---------------",
"library(flowr)\n",
"\n# --------------- Create a list of commands ---------------"
)
code_cmds <- lapply(1:nrow(def), function(i){
j <- def$jobname[i]
cmd <- mat$cmd[i]
code_cmd <- sprintf("%s = '%s'", j, cmd)
})
code_cmds <- sprintf("cmds <- list(%s)\n\t",
paste(sprintf("%s = '%s'", dat$jobname, dat$cmd), collapse = ",\n\t"))
code_flowmat <- c(
"\n# --------------- create a flowmat ---------------",
"flowmat <- to_flowmat(cmds, samplename = 'samp1')")
code_flowdef <- c(
"\n# --------------- create a flow definition ---------------",
"flowdef = to_flowdef(flowmat,",
sprintf("\tprev_jobs = c( '%s' ),", paste(dat$prev_jobs, collapse = "', '")),
sprintf("\ttcpu_reserved = c( '%s' ),", paste(dat$tcpu_reserved, collapse = "', '")),
sprintf("\tdep_type = c( '%s' ),", paste(dat$dep_type, collapse = "', '")),
sprintf("\tsub_type = c( '%s' )", paste(dat$sub_type, collapse = "', '")), ")")
code_final <- c(
"\n# --------------- submit the flow to HPCC---------------",
"fobj = to_flow(flowmat, flowdef, execute = TRUE, platform = 'local')",
"\n# --- plot, using flow object OR flowdef",
"plot_flow(flowdef)",
"plot_flow(fobj)",
"\n# --- other functions",
"status(fobj)",
"# kill(fobj)"
)
# code_jobs <- sapply(1:nrow(x), function(i){
# #prev_jobs=fobj@jobs[[j]]@previous_job;prev_jobs <- ifelse(length(prev_jobs) > 1, prev_jobs, "none")
# #cpu=fobj@jobs[[j]]@cpu;cmds=fobj@jobs[[j]]@cmds
# j <- x$jobnames[i]
# prev_jobs <- x$prev_jobs[i];
# cpu <- x$cpu[i];
# cmd <- x$cmd[i]
# dep_type <- x$dep_type[i]
# sub_type <- x$sub_type[i]
# code_cmd <- sprintf("cmd_%s <- '%s'", j, cmd)
# code_job <- sprintf("jobj_%s <- job(name = '%s', q_obj = qobj, previous_job = '%s',
# cpu = '%s', cmd = cmd_%s,
# submission_type = '%s', dependency_type = '%s')",
# j, j, prev_jobs, cpu, j, dep_type, sub_type)
# return(c(code_cmd, code_job))
# })
# code_flow <- sprintf("fobj <- flow(name = '%s', jobs = list(%s), desc='flow_instance_description')",
# input$flow_name, paste('jobj',jobnames,sep="_", collapse=", "))
# code_final <- c("\n\n#####----- Define the flow", code_flow,
# "#####----- plot the flow", "plot_flow(fobj)",
# "#####----- submit the flow and the files getting created", "submit(fobj, execute = FALSE)",
# "#####----- submit the flow and the files getting created", "submit(fobj, execute = TRUE)")
return(c(code_initial, code_cmds, code_flowmat, code_flowdef, code_final))
}