main.R 50 KB
Newer Older
1
2
#!/usr/bin Rscript
# This script will take a SAG yaml configuration file and generate a ShinyApp
3
4
# Accepts two parameters: path to sag yaml configuration file
# Usage: Rscript /path/to/sag.yaml /output/dir/
5
args = commandArgs(trailingOnly=TRUE)
jlopez's avatar
jlopez committed
6
7
8
source("tools.R")
library(yaml)

9
path_yml <- args[1]
10
output_dir <- args[2]
jlopez's avatar
jlopez committed
11

mmassaviol's avatar
mmassaviol committed
12
APP <<- yaml.load_file(path_yml,handlers = list("float#fix"=function(x){ format(x,scientific=F)}))$App
jlopez's avatar
jlopez committed
13
14

generate_added_styles <- function () {
15
  path_css = output_dir + "/www/added_styles.css"
jlopez's avatar
jlopez committed
16

jlopez's avatar
jlopez committed
17
  file.replace(path_css)
jlopez's avatar
jlopez committed
18
19
20
21
}

#============================================================

jlopez's avatar
jlopez committed
22
23
24
25
26
generate_page_input <- function(res, cnt) {
  
  if(cnt$type == "button") {
    
    if(cnt$icon == "") {
jlopez's avatar
jlopez committed
27
      res <- res + '\t\tactionButton("' + cnt$name + '", "'+ cnt$label +'", class="'+ cnt$class +'")' 
jlopez's avatar
jlopez committed
28
29
      
    } else {
jlopez's avatar
jlopez committed
30
      res <- res + '\t\tactionButton("' + cnt$name + '", "'+ cnt$label +'",  icon("'+ cnt$icon +'"), class="'+ cnt$class +'")' 
jlopez's avatar
jlopez committed
31
32
    }
    
33
34
  }
  if(cnt$type == "text") {
jlopez's avatar
jlopez committed
35
    
36
    res <- res + '\t\ttextInput("'+ cnt$name +'", label = "'+ cnt$label +'", value = "'+ format(cnt$value, scientific=F) +'", width = "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
37
    
mmassaviol's avatar
mmassaviol committed
38
39
40
41
42
  }
  if(cnt$type == "textArea") {
    
    res <- res + '\t\ttextAreaInput("'+ cnt$name +'", label = "'+ cnt$label +'", value = "'+ cnt$value +'")'
    
43
44
  }
  if(cnt$type == "numeric") {
45
    
46
    res <- res + '\t\tnumericInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = '+ format(cnt$value, scientific=F) +')'
47

48
49
  }
  if(cnt$type == "slider") {
jlopez's avatar
jlopez committed
50
    if(length(cnt$value) == 1) {
jlopez's avatar
jlopez committed
51
      res <- res + '\t\tsliderInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = '+ cnt$value +')'
jlopez's avatar
jlopez committed
52
    } else {
jlopez's avatar
jlopez committed
53
      res <- res + '\t\tsliderInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = c('+ cnt$value[1] + ',' + cnt$value[2] + '))'
jlopez's avatar
jlopez committed
54
    }
55
56
  }
  if(cnt$type == "help") {
jlopez's avatar
jlopez committed
57
    res <- res + '\t\tp("'+cnt$label+'")'
58
59
  }
  if(cnt$type == "link") {
mmassaviol's avatar
mmassaviol committed
60
    res <- res + '\t\tp("'+cnt$label+'",a(href="'+cnt$href+'","'+cnt$href+'",target="_blank"))'
61
62
  }
  if(cnt$type == "select") {
jlopez's avatar
jlopez committed
63
64
    
    
jlopez's avatar
jlopez committed
65
    res <- res + '\t\tselectInput("'+ cnt$name +'", label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
66
    
mmassaviol's avatar
mmassaviol committed
67
    cpt = 1
jlopez's avatar
jlopez committed
68
    if(length(cnt$choices) > 0) {
mmassaviol's avatar
mmassaviol committed
69
      for(ch in cnt$choices){
jlopez's avatar
jlopez committed
70
        
mmassaviol's avatar
mmassaviol committed
71
72
        if(cpt < length(cnt$choices)) {
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '", '
jlopez's avatar
jlopez committed
73
        } else {
mmassaviol's avatar
mmassaviol committed
74
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '"), '
jlopez's avatar
jlopez committed
75
        }
mmassaviol's avatar
mmassaviol committed
76
        cpt = cpt + 1
jlopez's avatar
jlopez committed
77
78
79
      }
    }
    
80
    res <- res + ' selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
81
    
82
83
  }
  if(cnt$type == "radio") {
jlopez's avatar
jlopez committed
84
    
jlopez's avatar
jlopez committed
85
    res <- res + '\t\tradioButtons("'+ cnt$name +'", label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
86
    
87
    cpt = 1
jlopez's avatar
jlopez committed
88
    if(length(cnt$choices) > 0) {
89
      for(ch in cnt$choices){
jlopez's avatar
jlopez committed
90
        
91
92
        if(cpt < length(cnt$choices)) {
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '", '
jlopez's avatar
jlopez committed
93
        } else {
94
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '"), '
jlopez's avatar
jlopez committed
95
        }
96
        cpt = cpt + 1
jlopez's avatar
jlopez committed
97
98
99
      }
    }
    
100
    res <- res + ' selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
101
    
102
103
  }
  if(cnt$type == "checkbox") {
jlopez's avatar
jlopez committed
104
    
jlopez's avatar
jlopez committed
105
    res <- res + '\t\tcheckboxInput("'+ cnt$name +'", label = "'+ cnt$label +'", value = ' + cnt$value + ')'  
jlopez's avatar
jlopez committed
106
    
107
108
  }
  if(cnt$type == "chooser") {
jlopez's avatar
jlopez committed
109
110
111
112
113
114
115
116
117
118
119
120
    
    vl <- 'c('
    vr <- 'c('
    
    if(length(cnt$value_left) > 0) {
      for(x in 1:length(cnt$value_left)) {
        vl <- vl + '"' + cnt$value_left[[x]] + '"'
        if(x < length(cnt$value_left)) {
          vl <- vl + ', '
        }
      }
    }
jlopez's avatar
jlopez committed
121
    
jlopez's avatar
jlopez committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    vl <- vl + ')'
    
    if(length(cnt$value_right) > 0) {
      vr <- "c("
      for(x in 1:length(cnt$value_right)) {
        vr <- vr + '"' + cnt$value_right[[x]] + '"'
        if(x < length(cnt$value_right)) {
          vr <- vr + ', '
        }
      }
    }
    
    vr <- vr + ')'
  
    res <- res + '\t\tchooserInput("' + cnt$name + '", "Available", "Selected", ' + vl + ', ' + vr + ', size = ' + cnt$size + ', multiple = ' + cnt$multiple + ')'

138
139
  }
  if(cnt$type == "checkboxGroupInput") {
jlopez's avatar
jlopez committed
140
    
jlopez's avatar
jlopez committed
141
    res <- res + '\t\tcheckboxGroupInput("'+ cnt$name +'",label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    
    if(length(cnt$choices) > 0) {
      for(ch in 1:length(cnt$choices)){
        
        choices = cnt$choices[[ch]]
        
        value = choices$value
        
        if(is.character(choices$value)) {
          value = '"' + choices$value + '"'
        }
        
        if(ch < length(cnt$choices)) {
          res <- res + '"' + choices$label + '"' + ' = ' + value + ', '
        } else {
          res <- res + '"' + choices$label + '"' + ' = ' + value
        }
      }
    }
    
162
    res <- res + '), selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
163
    
164
  }
mmassaviol's avatar
mmassaviol committed
165
  if(cnt$type == "input_file_client") {
jlopez's avatar
jlopez committed
166
    
jlopez's avatar
jlopez committed
167
    res <- res + '\t\tfileInput("'+ cnt$name +'", label = "'+ cnt$label + '"'
jlopez's avatar
jlopez committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    
    if(length(cnt$accept) > 0) {
      
      res <- res + ", accept = c("
      
      for(a in 1:length(cnt$accept)){
        form <- cnt$accept[[a]]
        
        res <- res + '"' + form + '"'
        
        
        if(a < length(cnt$accept)) {
          res <- res + ', '
        } else {
          res <- res + ')'
        }
        
      }
      
    }
    
jlopez's avatar
jlopez committed
189
    res <- res + ', multiple = ' + cnt$multiple + ', width =  "'+ 'auto' + '")'
jlopez's avatar
jlopez committed
190
191
    
  }
mmassaviol's avatar
mmassaviol committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  # output_file_server
  if(cnt$type == "input_file_server"){
    res <- res + '\t\ttags$label("' + cnt$label + '"),\n'
    res <- res + '\t\tfluidRow(\n'
    res <- res + '\t\t\tcolumn(4,shinyFilesButton("shinyfiles_' + cnt$name + '",label="Please select a file", title="' + cnt$label + '", multiple=FALSE)),\n'
    res <- res + '\t\t\tcolumn(8,textInput("' + cnt$name + '",label=NULL,value=""))\n'
    res <- res + '\t\t)\n'
  }
  if(cnt$type == "input_dir" || cnt$type == "output_dir"){
    res <- res + '\t\ttags$label("' + cnt$label + '"),\n'
    res <- res + '\t\tfluidRow(\n'
    res <- res + '\t\t\tcolumn(4,shinyDirButton("shinydir_' + cnt$name + '",label="Please select a directory", title="' + cnt$label + '")),\n'
    res <- res + '\t\t\tcolumn(8,textInput("' + cnt$name + '",label=NULL,value=""))\n'
    res <- res + '\t\t)\n'
  }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  if(cnt$type == "input_file"){
    res <- res + '\tbox(title = "' + cnt$label + '", width = 12, status = "success", collapsible = TRUE, solidHeader = TRUE,\n'
    res <- res + '\t\tselectInput("' + cnt$name + '_select",label = "Select where to find the file", selected = "server", choices = c("On server" = "server", "On your machine" = "local")),\n'
    res <- res + '\t\tconditionalPanel(condition = "input.' + cnt$name + '_select == \'server\'",\n'
    res <- res + '\t\t\t\ttags$label("' + cnt$label + '"),\n'
    res <- res + '\t\t\t\tfluidRow(\n'
    res <- res + '\t\t\t\t\tcolumn(4,shinyFilesButton("shinyfiles_' + cnt$name + '",label="Please select a file", title="' + cnt$label + '", multiple=FALSE)),\n'
    res <- res + '\t\t\t\t\tcolumn(8,textInput("' + cnt$name + '_server",label=NULL,value=""))\n'
    res <- res + '\t\t\t\t)\n'
    res <- res + '\t\t\t),\n'
    res <- res + '\t\tconditionalPanel(condition = "input.' + cnt$name + '_select == \'local\'",\n'
    res <- res + '\t\t\tfileInput("' + cnt$name + '_local",label = "' + cnt$label + '")\n'
    res <- res + '\t\t)\n'
    res <- res + '\t)\n'
  }
jlopez's avatar
jlopez committed
222
223
224
225
  
  return(res)
}

jlopez's avatar
jlopez committed
226
227
generate_page_box <- function(panel, box, res) {
  
mmassaviol's avatar
mmassaviol committed
228
  res <- res + 'box(title = "' + box$title + '", width = 12, status = "' + box$status + '", collapsible = TRUE, solidHeader = TRUE'
jlopez's avatar
jlopez committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  
  panel_content = box$content
  
  if(length(panel_content) > 0) {
    
    res <- res + ',\n'
    
    for(z in 1:length(panel_content)) {
      res <- generate_page_input(res, panel_content[[z]])
      
      if(z < length(panel_content)) {
        res <- res + ','
      }
  
      res <- res + '\n\n'
    }
  }
  
mmassaviol's avatar
mmassaviol committed
247
  res <- res + '\t)'
jlopez's avatar
jlopez committed
248
249
250
251
252
253
254
  
  return(res)
  
}

#============================================================

jlopez's avatar
update    
jlopez committed
255
generate_pages_server <- function() {
jlopez's avatar
jlopez committed
256
  resG <- ""
jlopez's avatar
update    
jlopez committed
257
258
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
259
    resG <- resG + "\t# \tPage : " + name + "\n"
260
    path_page = output_dir+"/pages/pages_def_"+tolower(name)+".R"
jlopez's avatar
jlopez committed
261
    file.replace(path_page)
jlopez's avatar
update    
jlopez committed
262
    
jlopez's avatar
jlopez committed
263
    res <- 'tab'+ name + ' = fluidPage(\n\n'
jlopez's avatar
jlopez committed
264
    
mmassaviol's avatar
mmassaviol committed
265
    res <- res + 'box(title = "Parameters :", width = 12, status = "primary", collapsible = TRUE, solidHeader = TRUE'
jlopez's avatar
jlopez committed
266
     
mmassaviol's avatar
mmassaviol committed
267
    content <- APP$pages[[x]]
jlopez's avatar
update    
jlopez committed
268
269
    
    if(length(content) > 0) {
jlopez's avatar
jlopez committed
270
      res <- res + ',\n\n'
mmassaviol's avatar
mmassaviol committed
271
272

      boxes = content$boxes
jlopez's avatar
jlopez committed
273
      
mmassaviol's avatar
mmassaviol committed
274
      if(length(boxes) > 0) {
jlopez's avatar
jlopez committed
275
        
mmassaviol's avatar
mmassaviol committed
276
        if(length(boxes) > 1){
277
          res <- res + '\tselectInput("select' + content$name + '", label = "Select the tool to use : ", selected = "' + content$default + '", choices = list(' 
jlopez's avatar
jlopez committed
278
          
mmassaviol's avatar
mmassaviol committed
279
280
          for(b in 1:length(boxes)) { # construction list box tools
            res <- res + '"' + boxes[[b]]$name +'" = "' + boxes[[b]]$name + '"'
jlopez's avatar
update    
jlopez committed
281
            
mmassaviol's avatar
mmassaviol committed
282
            if(b < length(boxes)) {
jlopez's avatar
jlopez committed
283
284
285
286
287
              res <- res + ', '
            }
            
          }
          res <- res + ')),\n'
mmassaviol's avatar
mmassaviol committed
288
289
290
291
292
293
        }
        else{
          res <- res + '\thidden(textInput("select' + content$name + '", label = "", value="'+ boxes[[1]]$name +'")),'
        }
        
        for(b in 1:length(boxes)) { # draw boxes
jlopez's avatar
update    
jlopez committed
294
          
mmassaviol's avatar
mmassaviol committed
295
296
297
298
299
300
          box <- boxes[[b]]
          
          if(length(boxes) > 1){
            res <- res + "\nconditionalPanel(condition = \"input.select"+content$name+" == '"+box$name+"'\","
          }

301
302
303
          #if(box$defined == TRUE) {
          #  box <- get_predefined_box(box, APP$pages[[x]]$name)
          #} 
mmassaviol's avatar
mmassaviol committed
304
305
306
307
308
309
310
311
          
          res <- generate_page_box(panel, box, res)

          if(length(boxes) > 1){
            res <- res + ")"
          }
          if(b < length(boxes)){
            res <- res + ","
jlopez's avatar
update    
jlopez committed
312
          }
jlopez's avatar
jlopez committed
313
        }
jlopez's avatar
update    
jlopez committed
314
315
316
      }
    }
    
mmassaviol's avatar
mmassaviol committed
317
    res <- res + '))\n\n'
jlopez's avatar
jlopez committed
318

jlopez's avatar
update    
jlopez committed
319
320
    write(res, file=path_page)
    
321
    path_opt = output_dir+"/server/opt_"+tolower(name)+".R"
322
    #file.replace(path_opt)
jlopez's avatar
update    
jlopez committed
323
324
325
    
    res <- ""
    
mmassaviol's avatar
mmassaviol committed
326
    content <- APP$pages[[x]]
jlopez's avatar
update    
jlopez committed
327
    
328
    # if(length(content) > 0) {
mmassaviol's avatar
mmassaviol committed
329
      
330
    #   boxes = content$boxes
mmassaviol's avatar
mmassaviol committed
331
      
332
    #   if(length(boxes) > 0) {
jlopez's avatar
jlopez committed
333
        
jlopez's avatar
jlopez committed
334
        
335
    #     for(b in 1:length(boxes)) {
jlopez's avatar
jlopez committed
336
          
337
    #       box <- boxes[[b]]
jlopez's avatar
jlopez committed
338
          
339
340
341
    #       #if(box$defined == TRUE) {
    #       #  box <- get_predefined_box(box, APP$pages[[x]]$name)
    #       #} 
jlopez's avatar
jlopez committed
342
          
jlopez's avatar
jlopez committed
343
          
344
    #       panel_content = box$content
mmassaviol's avatar
mmassaviol committed
345
346
  
      
347
    #       if(length(panel_content) > 0) {
mmassaviol's avatar
mmassaviol committed
348
      
349
    #     for(z in 1:length(panel_content)) {
mmassaviol's avatar
mmassaviol committed
350
          
351
    #       cnt <- panel_content[[z]]
mmassaviol's avatar
mmassaviol committed
352
          
353
354
    #       type <- cnt$type
    #       id <- cnt$name
mmassaviol's avatar
mmassaviol committed
355
          
356
357
358
    #       if(type == "button") {
    #           res <- res + '#\' Event when use '+ id +' button\n'
    #           res <- res + 'observeEvent(input$'+id+', {\n\n'
mmassaviol's avatar
mmassaviol committed
359
              
360
361
362
    #           res <- res + '})\n\n'
    #       }
    #     }
mmassaviol's avatar
mmassaviol committed
363
        
364
    #   }
jlopez's avatar
jlopez committed
365
      
366
    #     }
mmassaviol's avatar
mmassaviol committed
367
        
368
369
    #   }
    # }
jlopez's avatar
jlopez committed
370
    
mmassaviol's avatar
mmassaviol committed
371
372
373
    run <- APP$run

    if(length(run$options) > 0) {
jlopez's avatar
jlopez committed
374
      
375
376
      # res <- res + '#\' Event when use '+ run$shiny_button$name +' button\n'
      # res <- res + 'observeEvent(input$'+ run$shiny_button$name+', {\n\n'
jlopez's avatar
jlopez committed
377
      
378
      # page_path <- output_dir + '/params/' + 'params_' + APP$pages[[x]]$name + '.yml'
jlopez's avatar
jlopez committed
379
      
380
      # file.replace(page_path)
jlopez's avatar
jlopez committed
381
382
      
      
383
384
385
      # res <- res + '# Params \n'
      # res <- res + '\tpath_param <- "' + page_path + '"\n\n'
      # res <- res + '\tres <- ""\n'
jlopez's avatar
jlopez committed
386
      
387
      # res <- res + '\n\t# Page : ' + APP$pages[[x]]$name+ '\n'
mmassaviol's avatar
mmassaviol committed
388
389
390
391
      
      boxes = APP$pages[[x]]$boxes
      
      if(length(boxes) > 0) {
392

393
        resG <- resG + '\t\tres = paste0(res , paste("'+APP$pages[[x]]$name + ':", paste0(\'\"\', input$select' + APP$pages[[x]]$name + ', \'\"\'), "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
394
        for(b in 1:length(boxes)) {  # list boxes
jlopez's avatar
jlopez committed
395
          
mmassaviol's avatar
mmassaviol committed
396
397
          box <- boxes[[b]]
          
398
          # res <- res + '\n\t# Tool : ' + box$name+ '\n'
mmassaviol's avatar
mmassaviol committed
399
          
400
401
402
403
404
405
406
407
          # if (length(boxes) > 1){
          #   verif = "input$select" + APP$pages[[x]]$name + ' == "' + box$name + '"'
          # }
          # else{
          #   verif = "TRUE"
          # }

          # res <- res + '\n\tif('+verif+') {\n'
mmassaviol's avatar
mmassaviol committed
408
409
410
411
          
          panel_content = box$content
    
          if(length(panel_content) > 0) {
412

mmassaviol's avatar
mmassaviol committed
413
            for(z in 1:length(panel_content)) {
jlopez's avatar
jlopez committed
414
              
mmassaviol's avatar
mmassaviol committed
415
416
417
418
              cnt <- panel_content[[z]]
              
              type <- cnt$type
              id <- cnt$name
jlopez's avatar
jlopez committed
419
              
mmassaviol's avatar
mmassaviol committed
420
421
              genR <- ""
            
mmassaviol's avatar
mmassaviol committed
422
              if(cnt$type == 'text' || cnt$type == 'textArea' || cnt$type == "input_dir" || cnt$type == "output_dir") {
423
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
424
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", format(input$' + id + ', scientific=F), "\\n", sep = " "))\n'
425
426
427
428
                genR <-  genR + '\t\t} else {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
429
430
              }
              if(cnt$type == 'numeric') {
431
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
432
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", format(input$' + id + ', scientific=F), "\\n", sep = " "))\n'
433
434
435
436
                genR <-  genR + '\t\t} else {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
437
438
              }
              if(cnt$type == "slider") {
mmassaviol's avatar
mmassaviol committed
439
                if(length(cnt$value) == 1) {
440
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))'
jlopez's avatar
jlopez committed
441
                  genR <-  genR + '\t\n\n'
jlopez's avatar
jlopez committed
442
                } else {
443
444
445
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_min:", input$' + id + '[1], "\\n", sep = " "))\n'
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_max:", input$' + id + '[2], "\\n", sep = " "))\n'
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_step:", "' + cnt$step + '", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
446
                  genR <-  genR + '\t\n\n'
jlopez's avatar
jlopez committed
447
                }
mmassaviol's avatar
mmassaviol committed
448
449
              }
              if(cnt$type == "select") {
mmassaviol's avatar
mmassaviol committed
450
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
451
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
452
                genR <-  genR + '\t\t} else {\n'
453
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
454
455
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
456
457
              }
              if(cnt$type == "file") {
458
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + '$datapath, \'\"\'), "\\n", sep = " "))'
mmassaviol's avatar
mmassaviol committed
459
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
460
461
              }
              if(cnt$type == "checkbox") {
mmassaviol's avatar
mmassaviol committed
462
                genR <-  genR + '\t\tif(input$' + id + ') {\n'
463
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", "true", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
464
                genR <-  genR + '\t\t} else {\n'
465
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", "false", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
466
467
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
468
469
              }
              if(cnt$type == "chooser") {
mmassaviol's avatar
mmassaviol committed
470
471
472
473
474
475
476
477
478
479
                genR <-  genR + '\t\tres <- paste0(res, " ' + id + '" + "_left: [")\n'
                genR <-  genR + '\t\tif(length(input$'+ id +'$left) > 0) {\n' 
                genR <-  genR + '\t\tfor(x in 1:length(input$'+ id +'$left)) {\n'
                genR <-  genR + '\t\t\tres <- paste0(res, \'"\', input$'+ id +'$left[[x]], \'"\')\n'
                genR <-  genR + '\t\t\tif(x < length(input$'+ id +'$left)) {\n'
                genR <-  genR + '\t\t\t\tres <- paste0(res, ", ")\n'
                genR <-  genR + '\t\t\t}\n'
                genR <-  genR + '\t\t}\n'
                genR <-  genR + '\t\t}		\n'
                genR <-  genR + '\t\tres <- paste0(res, "]", "\\n")\n\n'
jlopez's avatar
jlopez committed
480
                
mmassaviol's avatar
mmassaviol committed
481
482
483
484
485
486
487
488
489
490
                genR <-  genR + '\t\tres <- paste0(res, " ' + id + '" + "_right: [")\n'
                genR <-  genR + '\t\tif(length(input$'+ id +'$right) > 0) {\n' 
                genR <-  genR + '\t\tfor(x in 1:length(input$'+ id +'$right)) {\n'
                genR <-  genR + '\t\t\tres <- paste0(res, \'"\', input$'+ id +'$right[[x]], \'"\')\n'
                genR <-  genR + '\t\t\tif(x < length(input$'+ id +'$right)) {\n'
                genR <-  genR + '\t\t\t\tres <- paste0(res, ", ")\n'
                genR <-  genR + '\t\t\t}\n'
                genR <-  genR + '\t\t}\n'
                genR <-  genR + '\t\t}		\n'
                genR <-  genR + '\t\tres <- paste0(res, "]", "\\n")\n\n'
jlopez's avatar
jlopez committed
491
                
mmassaviol's avatar
mmassaviol committed
492
493
              }
              if(cnt$type == "radio") {
494
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
495
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
496
                genR <-  genR + '\t\t} else {\n'
497
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
498
499
500
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
              }
501
502
503
504
505
506
507
508
509
510
              if(cnt$type == "input_file") {
                genR <- genR + '\t\tres = paste0(res, paste("' + id + '_select:", paste0(\'\"\', input$' + id + '_select, \'\"\'), "\\n", sep = " "))\n'
                genR <- genR + '\t\tif(input$' + id + '_select == "server") {\n'
                genR <- genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + '_server, \'\"\'), "\\n", sep = " "))\n'
                genR <- genR + '\t\t}\n'
                genR <- genR + '\t\tif(input$' + id + '_select == "local"){\n'
                genR <- genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + '_local$datapath, \'\"\'), "\\n", sep = " "))\n'
                genR <- genR + '\t\t}'
                genR <- genR + '\t\n\n'
              }
511
512
              
              else {
mmassaviol's avatar
mmassaviol committed
513
                #TODO
jlopez's avatar
jlopez committed
514
              }
mmassaviol's avatar
mmassaviol committed
515
              
516
              # res <- res + genR
mmassaviol's avatar
mmassaviol committed
517
              resG <- resG + genR
jlopez's avatar
jlopez committed
518
            }
mmassaviol's avatar
mmassaviol committed
519
          
jlopez's avatar
jlopez committed
520
          }
mmassaviol's avatar
mmassaviol committed
521
      
522
          # res <- res + '\t}\n'
jlopez's avatar
jlopez committed
523
          
524
          # res <- res + '\n'
mmassaviol's avatar
mmassaviol committed
525
      
jlopez's avatar
jlopez committed
526
        }
mmassaviol's avatar
mmassaviol committed
527
        
jlopez's avatar
jlopez committed
528
529
      }
      
530
      # res <- res + '\twrite(res, file=path_param)'
jlopez's avatar
jlopez committed
531
      
532
      # res <- res + '\n\n'
jlopez's avatar
jlopez committed
533
      
534
      # res <- res + '})\n\n'
jlopez's avatar
update    
jlopez committed
535
536
      
    }
mmassaviol's avatar
mmassaviol committed
537
    #write(res, file=path_opt)
jlopez's avatar
update    
jlopez committed
538
  }
jlopez's avatar
jlopez committed
539
  
540
541
542
  save_params <- "save_params <- function(path_param){\n"
  save_params <- save_params + '\tres = ""'
  save_params <- save_params + resG
543
  #save_params <- save_params + '\t\tres = paste0(res, paste("final_step:", paste0(\'\"\', input$final_step, \'\"\'), "\\n", sep = " "))\n\n'
mmassaviol's avatar
mmassaviol committed
544
  save_params <- save_params + '\ta = yaml.load_file("/workflow/params.total.yml", handlers=list("float#fix"=function(x){ format(x,scientific=F)}))\n'
mmassaviol's avatar
mmassaviol committed
545
546
  save_params <- save_params + '\tp = a[["params"]]\n'
  save_params <- save_params + '\ta["params"] = NULL\n'
mmassaviol's avatar
mmassaviol committed
547
  save_params <- save_params + '\tb = yaml.load(res, handlers=list("float#fix"=function(x){ format(x,scientific=F)}))\n'
mmassaviol's avatar
mmassaviol committed
548
549
550
  save_params <- save_params + '\tpnotb = subset(names(p), !(names(p)%in%names(b)))\n'
  save_params <- save_params + '\td = list()\n'
  save_params <- save_params + '\td$params = c(p[pnotb],b)\n'
551
552
553
554
555
  save_params <- save_params + '\tlogical = function(x) {\n'
  save_params <- save_params + '\t\tresult <- ifelse(x, "True", "False")\n'
  save_params <- save_params + '\t\tclass(result) <- "verbatim"\n'
  save_params <- save_params + '\t\treturn(result)\n'
  save_params <- save_params + '\t}\n'
mmassaviol's avatar
mmassaviol committed
556
  save_params <- save_params + '\td = c(d,a)\n'
557
558
559
560
  #save_params <- save_params + '\tget_samples = yaml.load(system(paste0("python3 /workflow/get_samples.py ",input$sample_dir," ",input$SeOrPe),intern = T))\n'
  #save_params <- save_params + '\td$samples = get_samples$samples\n'
  #save_params <- save_params + '\td$params$sample_suffix = get_samples$suffix\n'
  save_params <- save_params + '\twrite_yaml(d,path_param,handlers=list(logical = logical,"float#fix"=function(x){ format(x,scientific=F) }))\n'
561
562
563
564
  save_params <- save_params + '\t}\n\n'

  res <- save_params

565
566
567
  res <- res + 'force_rule <- function(force_from){\n'
  res <- res + '\tif (input$force_from=="none"){\n'
  res <- res + '\t\treturn("")\n'
568
  res <- res + '\t}\n'
569
570
  res <- res + '\telse if (input$force_from=="all"){ return("--forcerun all") }\n'
  res <- res + '\telse {\n'
571
  res <- res + '\t\tparams = yaml.load_file(paste0(input$results_dir,"/params.yml"), handlers=list("float#fix"=function(x){ format(x,scientific=F)}))\n'
572
573
  res <- res + '\t\toutputs = params[["outputs"]]\n'
  res <- res + '\t\ttool = params[["params"]][[force_from]]\n'
574
575
576
  res <- res + '\t\tsteptool = paste0(force_from,"__",tool)\n'
  res <- res + '\t\tif (length(outputs[[steptool]])==1)\n'
  res <- res + '\t\t\trule = names(outputs[[steptool]])[[1]]\n'
577
  res <- res + '\t\telse{\n'
578
  res <- res + '\t\t\trule = names(outputs[[steptool]])[[grep(input$SeOrPe,names(outputs[[steptool]]))]]\n'
579
  res <- res + '\t\t}\n'
580
  res <- res + '\t\treturn(paste0("--forcerun ",rule))\n'
581
582
583
  res <- res + '\t}\n'
  res <- res + '}\n'

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  # # Compare parameters (give rules that need to be re-runned)
  # res <- res + 'compare_params = function(dossierAnalyse){\n'
  # res <- res + '\tif (!file.exists(paste0(dossierAnalyse,"/lastrun/params.yml"))){\n'
  # res <- res + '\t\treturn(c())\n'
  # res <- res + '\t}\n'
  # res <- res + '\telse{\n'
  # res <- res + '\t\tnew_params = yaml.load_file(paste0(dossierAnalyse,"/params.yml"))\n'
  # res <- res + '\t\told_params = yaml.load_file(paste0(dossierAnalyse,"/workflow/params.yml"))\n'
  # res <- res + '\t\tchanged = new_params[!(new_params %in% old_params)]\n'
  # res <- res + '\t\trules = c()\n'
  # res <- res + '\t\tif (length(changed)>=1){\n'
  # res <- res + '\t\t\tfor (param in names(changed)){\n'
  # res <- res + '\t\t\t\tif (!grepl("_threads$",param)){\n'
  # res <- res + '\t\t\t\t\trules = c(rules, new_params$params_info[[param]]$rule)\n'
  # res <- res + '\t\t\t\t}\n'
  # res <- res + '\t\t\t}\n'
  # res <- res + '\t\t}\n'
  # res <- res + '\t\treturn(unique(rules))\n'
  # res <- res + '\t}\n'
  # res <- res + '}\n'

605
  file.replace(output_dir+"/server/opt_global.R")
606
  #res <- ""
607
608
  # Event RULEGRAPH
  if ("RULEGRAPH" %in% names(APP)){
609
610
    res <- res + '#\' Event when use RULEGRAPH button\n'
    res <- res + 'observeEvent({c(input$sidebarmenu,input$refresh_rg)}, {\n\n'
611
612
613
    res <- res + '\tif (!dir.exists(paste0(input$results_dir,"/logs"))){\n'
    res <- res + '\t\tdir.create(paste0(input$results_dir,"/logs"))\n'
    res <- res + '\t}\n'
614
615
616
617
618
    res <- res + '\tif(input$sidebarmenu=="RULEGRAPH"){\n'
    res <- res + '\t\tinput_list <- reactiveValuesToList(input)\n'
    res <- res + '\t\ttoggle_inputs(input_list,F,F)\n'
    res <- res + '\t\tpath_param <- paste0(input$results_dir,"/params.yml")\n\n'
    
619
    res <- res + '\t\tsave_params(path_param)\n'
620
621
622
623
624

	  res <- res + '\t\ti = sample.int(1000,size = 1)\n\n'
	  res <- res + '\t\tsystem(paste0("rm ",input$results_dir,"/rulegraph*"))\n\n'

    res <- res + '\t\toutUI = tryCatch({\n'
625
    res <- res + '\t\t\tsystem(paste0("snakemake -s /workflow/Snakefile --configfile ",input$results_dir,"/params.yml -d ",input$results_dir," all --rulegraph 1> ",input$results_dir,"/rulegraph",i,".dot 2> ",input$results_dir,"/logs/rulegraph.txt"),intern=T)\n'
626
627
628
629
    res <- res + '\t\t\tsystem(paste0("cat ",input$results_dir,"/rulegraph",i,".dot | dot -Tsvg -Gratio=0.75 > ",input$results_dir,"/rulegraph",i,".svg"),intern=T)\n'
    res <- res + '\t\t\ttagList(img(src = paste0("results/rulegraph",i,".svg") ,alt = "Rulegraph of Snakemake jobs",style="max-width: 100%;height: auto;display: block;margin: auto"))},\n'
    res <- res + '\t\terror = function(e){\n'
    res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
630
    res <- res + '\t\t\treturn(tags$p(HTML(paste(readLines(paste0(input$results_dir,"/logs/rulegraph.txt"),warn=F), collapse = "<br/>"))))},\n'
631
632
    res <- res + '\t\twarning = function(w){\n'
    res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
633
    res <- res + '\t\t\treturn(tags$p(HTML(paste(readLines(paste0(input$results_dir,"/logs/rulegraph.txt"),warn=F), collapse = "<br/>"))))})\n'
634
635
636
637
638

    res <- res + '\t\taddResourcePath("results", input$results_dir)\n'
    res <- res + '\t\toutput$RULEGRAPH_svg = renderUI(outUI)\n'
    res <- res + '\t\ttoggle_inputs(input_list,T,F)\n'
    res <- res + '}})\n'
639
  }
mmassaviol's avatar
mmassaviol committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660

  # Event dry_run
  res <- res + 'observeEvent({c(input$sidebarmenu,input$dry_run_button)}, {\n'
  res <- res + '\n'
  res <- res + '\tif (!dir.exists(paste0(input$results_dir,"/logs"))){\n'
  res <- res + '\t\tdir.create(paste0(input$results_dir,"/logs"))\n'
  res <- res + '\t}\n'
  res <- res + '\tif(input$sidebarmenu=="RULEGRAPH"){\n'
  res <- res + '\t\tinput_list <- reactiveValuesToList(input)\n'
  res <- res + '\t\ttoggle_inputs(input_list,F,F)\n'
  res <- res + '\t\tpath_param <- paste0(input$results_dir,"/params.yml")\n'
  res <- res + '\n'
  res <- res + '\t\tsave_params(path_param)\n'
  res <- res + '\n'
  res <- res + '\t\tforce = force_rule(input$force_from)\n'
  res <- res + '\t\tdry_out = system(paste0("snakemake -s /workflow/Snakefile --configfile ", paste0(input$results_dir,"/params.yml") ,\t" -d ", input$results_dir ,\t" --cores ", input$cores, " all ", force, " -n -p"),wait = T, intern=T)\n'
  res <- res + '\t\tdryUI = tags$p(HTML(paste(dry_out, collapse = "<br/>")))\n'
  res <- res + '\n'
  res <- res + '\t\toutput$dry_run_div = renderUI(dryUI)\n'
  res <- res + '\t\ttoggle_inputs(input_list,T,F)\n'
  res <- res + '\t}})\n'
661
  
mmassaviol's avatar
mmassaviol committed
662
  # Event Run
663
  res <- res + '#\' Event when use '+ run$shiny_button$name +' button\n'
mmassaviol's avatar
mmassaviol committed
664
  res <- res + 'observeEvent(input$'+run$shiny_button$name+', {\n\n'
665

mmassaviol's avatar
mmassaviol committed
666
  res <- res + '\trv$running = T\n'
667
668
  res <- res + '\tinput_list <- reactiveValuesToList(input)\n'
  res <- res + '\ttoggle_inputs(input_list,F,F)\n'
669
  res <- res + '\tupdateTabsetPanel(session, "sidebarmenu", selected = "run_out")\n'
670
  
671
  #panel_path <- output_dir + '/params/' + 'params_' + APP$pages[[x]]$name + '.yml'
mmassaviol's avatar
mmassaviol committed
672
  res <- res + '\tpath_param <- paste0(input$results_dir,"/params.yml")\n\n'
jlopez's avatar
jlopez committed
673
  
674
  res <- res + '\t\tsave_params(path_param)\n'
675
676
  res <- res + '\n\n'

677
  res <- res + '\toutUI = tryCatch({\n'
mmassaviol's avatar
mmassaviol committed
678
  res <- res + '\t\tif (!dir.exists(paste0(input$results_dir,"/logs"))){\n'
679
  res <- res + '\t\t\tdir.create(paste0(input$results_dir,"/logs"))\n'
680
  res <- res + '\t\t}\n'
681
682
  res <- res + '\t\tif (!file.exists(paste0(input$results_dir,"/logs/runlog.txt"))){\n'
  res <- res + '\t\t\tfile.create(paste0(input$results_dir,"/logs/runlog.txt"))\n'
683
  res <- res + '\t\t}\n'
mmassaviol's avatar
mmassaviol committed
684
685
686
687
688
689
  res <- res + '\t\tif (file.exists(paste0(input$results_dir,"/logs/workflow_end.ok"))){\n'
  res <- res + '\t\t\tfile.remove(paste0(input$results_dir,"/logs/workflow_end.ok"))\n'
  res <- res + '\t\t}\n'
  res <- res + '\t\tif (file.exists(paste0(input$results_dir,"/logs/workflow_end.error"))){\n'
  res <- res + '\t\t\tfile.remove(paste0(input$results_dir,"/logs/workflow_end.error"))\n'
  res <- res + '\t\t}\n'
690

691
692
693
694
695
696
697
698
699
700
701
702
  # res <- res + '\t\tforcerun = compare_params(input$results_dir)\n'
  # res <- res + '\t\tif (length(forcerun>1)){\n'
  # res <- res + '\t\t\trules = paste(forcerun, collapse=" ")\n'
  # res <- res + '\t\t\tforcerun = paste(" --forcerun ",rules)\n'
  # res <- res + '\t\t\tshowModal(modalDialog(\n'
  # res <- res + '\t\t\t\ttitle = "Params have changed since the last run",\n'
  # res <- res + '\t\t\t\tforcerun\n'
  # res <- res + '\t\t\t))\n'
  # res <- res + '\t\t}\n'
  # res <- res + '\t\telse{\n'
  # res <- res + '\t\t\tforcerun = ""\n'
  # res <- res + '\t\t}\n'
703

704
  res <- res + '\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow.running"),wait = T)\n'
mmassaviol's avatar
mmassaviol committed
705
706

  # RULEGRAPH
mmassaviol's avatar
mmassaviol committed
707
  res <- res + '\t\t\tsystem(paste0("snakemake -s /workflow/Snakefile --configfile ",input$results_dir,"/params.yml -d ",input$results_dir," all --rulegraph | dot -Tpng -Gratio=0.75 > ",input$results_dir,"/Rule_graph_mqc.png"),wait = T)\n'
mmassaviol's avatar
mmassaviol committed
708
709

  # RUN
710
  res <- res + '\t\tforce = force_rule(input$force_from)\n'
mmassaviol's avatar
mmassaviol committed
711
  res <- res + '\t\trerun = if (input$rerun_incomplete) "--rerun-incomplete" else ""\n'
mmassaviol's avatar
mmassaviol committed
712
713
714
715
716

    ## DRY RUN
    res <- res + '\t\t\tsystem(paste0("snakemake -s /workflow/Snakefile --configfile ", paste0(input$results_dir,"/params.yml") ," -d ", input$results_dir ," --cores ", input$cores, " all ", force, " ", rerun, " -n -p > ", input$results_dir, "/Run_Summary_mqc.html"),wait = T)\n'
    res <- res + '\t\t\tsystem(paste0("sed -i \'1 i\\\\<pre> \' ",input$results_dir,"/Run_Summary_mqc.html"))\n'

mmassaviol's avatar
mmassaviol committed
717
  res <- res + '\t\tsystem2("python3",paste0("-u -m snakemake -s /workflow/Snakefile --configfile ", paste0(input$results_dir,"/params.yml") ,\t" -d ", input$results_dir ,\t" --cores ", input$cores, " all ", force, " ",rerun),wait = FALSE, stdout = paste0(input$results_dir,"/logs/runlog.txt"), stderr = paste0(input$results_dir,"/logs/runlog.txt"))\n'
718
719
720
721
722
723
724
  res <- res + '\t\ttags$iframe(src="results/multiqc_report.html",width="100%", height="900px")},\n'
  res <- res + '\t\terror = function(e){\n'
  res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
  res <- res + '\t\t\treturn(tags$p(paste0("error : ",e$message)))},\n'
  res <- res + '\t\twarning = function(w){\n'
  res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
  res <- res + '\t\t\treturn(tags$p(paste0("error : ",w$message)))})\n'
mmassaviol's avatar
mmassaviol committed
725
726
727
728
729
  res <- res + '\t\t})\n\n'

  for(page in APP$pages){
    for(box in page$boxes){
      for(cnt in box$content){
mmassaviol's avatar
mmassaviol committed
730
731
732
733
734
735

        if ("prompt" %in% names(cnt)){
          #res <- res + '\t\t'
          res <- res + '\t\tobserveEvent({input$'+cnt$name+'},{showModal(modalDialog(title = "Info","'+cnt$prompt+'",easyClose = TRUE))},ignoreInit=TRUE)\n'
        }

736
737
        if ("volumes" %in% names(cnt)){
          volumes = 'c('
738
739
740
741
742
          for (volume in cnt$volumes){
            key = names(volume)
            val = unlist(volume)
            volumes = paste0(volumes,key,'="',val,'",')
          }
743
744
745
746
747
748
749
          volumes = substr(volumes, 1, nchar(volumes)-1)
          volumes = paste0(volumes,')')
        }
        else{
          volumes = 'c(Data="/Data",Results="/Results")'
        }

750
        if (cnt$type == "input_file"){
mmassaviol's avatar
mmassaviol committed
751
752
          res <- res + '\t\tshinyFileChoose(input, "shinyfiles_' + cnt$name + '", root=' + volumes + ',session = session)\n'
          res <- res + '\t\tobserveEvent({parseFilePaths(' + volumes + ',input$shinyfiles_' + cnt$name + ')$datapath[1]},{\n'
753
754
755
          res <- res + '\t\t\tif(length(parseFilePaths(' + volumes + ',input$shinyfiles_' + cnt$name + ')$datapath)==1){\n'
          res <- res + '\t\t\t\tupdateTextInput(session,"' + cnt$name + '_server",value = parseFilePaths(' + volumes + ',input$shinyfiles_' + cnt$name + ')$datapath[[1]])\n'
          res <- res + '\t\t\t}\n'
mmassaviol's avatar
mmassaviol committed
756
757
758
759
760
761
762
763
764
765
766
          res <- res + '\t\t})\n\n'
        }
        if (cnt$type == "input_dir" || cnt$type == "output_dir"){
          res <- res + '\t\tshinyDirChoose(input, "shinydir_' + cnt$name + '", root=' + volumes + ',session = session)\n'
          res <- res + '\t\tobserveEvent({parseDirPath(' + volumes + ',input$shinydir_' + cnt$name + ')},{\n'
          res <- res + '\t\t\tupdateTextInput(session,"' + cnt$name + '",value = parseDirPath(' + volumes + ',input$shinydir_' + cnt$name + '))\n'
          res <- res + '\t\t})\n\n'
        }
      }
    }
  }
mmassaviol's avatar
mmassaviol committed
767

768
  write(res, output_dir+"/server/opt_global.R")
jlopez's avatar
update    
jlopez committed
769
770
771
772
}

#============================================================

jlopez's avatar
jlopez committed
773
774
generate_menu <- function() {
  
775
  path_file = output_dir + "/R/menugauche.R"
jlopez's avatar
jlopez committed
776
  
jlopez's avatar
jlopez committed
777
  file.replace(path_file)
jlopez's avatar
jlopez committed
778
779
780
  
  res <- ""
  
jlopez's avatar
jlopez committed
781
  res <- res + 'MenuGauche = sidebarMenu(id="sidebarmenu",\n\n'
jlopez's avatar
jlopez committed
782
  
jlopez's avatar
update    
jlopez committed
783
784
785
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
    icon <- APP$pages[[x]]$icon
jlopez's avatar
jlopez committed
786
    menu <- APP$pages[[x]]$label
mmassaviol's avatar
mmassaviol committed
787
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
jlopez's avatar
update    
jlopez committed
788
  }
789

790
791
  if("RULEGRAPH" %in% names(APP)){
    name <- "RULEGRAPH"
792
    icon <- "gear"
mmassaviol's avatar
mmassaviol committed
793
    menu <- "Draw workflow graph"
mmassaviol's avatar
mmassaviol committed
794
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
795
  }
796

mmassaviol's avatar
mmassaviol committed
797
798
799
800
801
802
803
804
805
  if("download" %in% names(APP)){

    res <- res + '\ttags$br(),\n\n'

    res <- res + '\tdownloadButton("'+ APP$download$shiny_button$name +'", "'+ APP$download$shiny_button$label +'", class="'+ APP$download$shiny_button$class +'", style="color:black;margin: 6px 5px 6px 15px;"),\n\n'

    res <- res + '\ttags$br(),\n\n'
  }

mmassaviol's avatar
mmassaviol committed
806
  res <- res + '\ttags$br(),\n\n'
807

808
  res <- res + '\tnumericInput("cores", label = "Threads available", min = 1, max = 24, step = 1, width =  "auto", value = 16),\n'
809
  
810
  # select where to run again from
mmassaviol's avatar
mmassaviol committed
811
  choices = "list('No'='none'"
812
  for(step in APP$pages[2:length(APP$pages)]){ # all steps but remove global_params page
mmassaviol's avatar
mmassaviol committed
813
    choices <- choices + ",'" + step$label + "'='" + step$name + "'"
814
  }
mmassaviol's avatar
mmassaviol committed
815
  res <- res + 'selectInput("force_from", label = "Start again from a step : ", selected = "none", choices = '+choices+')),'
816
  
817
  #
818
819

  res <- res + '\ttags$br(),\n'
jlopez's avatar
jlopez committed
820
  
mmassaviol's avatar
mmassaviol committed
821
  res <- res + '\tactionButton("'+ APP$run$shiny_button$name +'", "'+ APP$run$shiny_button$label +'",  icon("'+ APP$run$shiny_button$icon +'"), class="'+ APP$run$shiny_button$class +'"),\n\n'
822
823
  res <- res + '\tactionButton("StopPipeline", "Stop pipeline",  icon("stop"), class="btn btn-secondary"),\n'
  
mmassaviol's avatar
mmassaviol committed
824
825
826
  res <- res + '\ttags$br(),\n'
  res <- res + '\ttags$br(),\n'

827
  res <- res + '\tmenuItem("Running Workflow output", tabName="run_out", icon=icon("terminal", lib="font-awesome"), newtab=FALSE),\n'
mmassaviol's avatar
mmassaviol committed
828

829
830
831
  if("Report" %in% names(APP)){
    name <- "Report"
    icon <- "file"
832
    menu <- "Final report"
mmassaviol's avatar
mmassaviol committed
833
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
834
  }
835
836
837

  res <- res + '\ttags$br(),\n'
  res <- res + '\tactionButton("close_session", "Close session",  icon("times"), class="btn btn-primary"),\n'
838
  
mmassaviol's avatar
mmassaviol committed
839
  res <- res + '\ttags$br(),tags$br(),\n\n'
jlopez's avatar
jlopez committed
840
  
mmassaviol's avatar
mmassaviol committed
841
  res <- res + '\tmenuItem("Powered by mbb", href="http://mbb.univ-montp2.fr/MBB/index.php", newtab=TRUE, icon=icon("book", lib="font-awesome"), selected=NULL)\n\n'
jlopez's avatar
jlopez committed
842
  
jlopez's avatar
jlopez committed
843
  res <- res + ')\n\n'
jlopez's avatar
jlopez committed
844
845
846
847
848
849
850
  
  write(res, file = path_file)
  
}

#============================================================

jlopez's avatar
jlopez committed
851
852
853
854
855
856
dir.create.not.exist <- function(path) {
  if(!dir.exists(path)) {
    dir.create(path)
  }
}

jlopez's avatar
jlopez committed
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
#============================================================

generate_rproj <- function(path) {
  file.replace(path)
  res <- ""
  res <- res + "Version: 1.0\n"
  res <- res + " \n"
  res <- res + "RestoreWorkspace: Default\n"
  res <- res + "SaveWorkspace: Default\n"
  res <- res + "AlwaysSaveHistory: Default\n"
  res <- res + " \n"
  res <- res + "EnableCodeIndexing: Yes\n"
  res <- res + "UseSpacesForTab: Yes\n"
  res <- res + "NumSpacesForTab: 2\n"
  res <- res + "Encoding: UTF-8\n"
  res <- res + " \n"
  res <- res + "RnwWeave: Sweave\n"
  res <- res + "LaTeX: pdfLaTeX\n"
  res <- res + " \n"
  write(res, path)
}

#============================================================

jlopez's avatar
jlopez committed
881
882
883
884
generate <- function() {
  
  print("Generate Start")
  
mmassaviol's avatar
mmassaviol committed
885
  #YML_TOOLS <<- get_predefined_tool(files_tools)
886
  
887
888
  if(dir.exists(output_dir)) {
    #unlink(output_dir, recursive=TRUE)
jlopez's avatar
jlopez committed
889
890
  }
  
891
  dir.create.not.exist(output_dir)
jlopez's avatar
jlopez committed
892
893
  generate_app()
  
894
  dir.create.not.exist(output_dir + "/R")
jlopez's avatar
jlopez committed
895
896
  generate_menu()
  
897
898
899
  dir.create.not.exist(output_dir + "/pages")
  dir.create.not.exist(output_dir + "/server")
  #dir.create.not.exist(output_dir + "/params")
jlopez's avatar
update    
jlopez committed
900
901
  
  generate_pages_server()
jlopez's avatar
jlopez committed
902

mmassaviol's avatar