annotate rmarkdown_flagstat_render.R @ 0:ba913054bbff draft

planemo upload
author mingchen0919
date Tue, 26 Dec 2017 19:27:41 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
1 library(getopt)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
2 library(rmarkdown)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
3 library(htmltools)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
4 library(dplyr)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
5
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
6 ##============ Sink warnings and errors to a file ==============
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
7 ## use the sink() function to wrap all code within it.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
8 ##==============================================================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
9 zz = file('warnings_and_errors.txt')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
10 sink(zz)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
11 sink(zz, type = 'message')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
12 ##---------below is the code for rendering .Rmd templates-----
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
13
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
14 ##=============STEP 1: handle command line arguments==========
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
15 ##
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
16 ##============================================================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
17 # column 1: the long flag name
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
18 # column 2: the short flag alias. A SINGLE character string
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
19 # column 3: argument mask
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
20 # 0: no argument
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
21 # 1: argument required
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
22 # 2: argument is optional
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
23 # column 4: date type to which the flag's argument shall be cast.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
24 # possible values: logical, integer, double, complex, character.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
25 #-------------------------------------------------------------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
26 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
27 # 1. short flag alias should match the flag in the command section in the XML file.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
28 # 2. long flag name can be any legal R variable names
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
29 # 3. two names in args_list can have common string but one name should not be a part of another name.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
30 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems.
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
31 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
32 args_list=list()
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
33 ##------- 1. input data ---------------------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
34 args_list$ECHO = c('echo', 'e', '1', 'character')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
35 ##--------2. output report and outputs --------------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
36 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
37 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
38 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
39 ##--------3. .Rmd templates in the tool directory ----------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
40 args_list$TOOL_TEMPLATE_RMD = c('tool_template_rmd', 't', '1', 'character')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
41 ##-----------------------------------------------------------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
42 opt = getopt(t(as.data.frame(args_list)))
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
43
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
44
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
45
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
46 ##=======STEP 2: create report directory (optional)==========
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
47 ##
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
48 ##===========================================================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
49 dir.create(opt$report_dir)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
50
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
51 ##=STEP 3: replace placeholders in .Rmd with argument values=
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
52 ##
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
53 ##===========================================================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
54 #++ need to replace placeholders with args values one by one+
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
55 readLines(opt$tool_template_rmd) %>%
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
56 (function(x) {
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
57 gsub('ECHO', opt$echo, x)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
58 }) %>%
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
59 (function(x) {
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
60 gsub('REPORT_DIR', opt$output_dir, x)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
61 }) %>%
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
62 (function(x) {
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
63 fileConn = file('tool_template.Rmd')
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
64 writeLines(x, con=fileConn)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
65 close(fileConn)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
66 })
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
67
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
68
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
69 ##=============STEP 4: render .Rmd templates=================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
70 ##
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
71 ##===========================================================
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
72 render('tool_template.Rmd', output_file = opt$report_html)
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
73
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
74
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
75 ##--------end of code rendering .Rmd templates----------------
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
76 sink()
ba913054bbff planemo upload
mingchen0919
parents:
diff changeset
77 ##=========== End of sinking output=============================