Mercurial > repos > mingchen0919 > rmarkdown_bdss_client_main
comparison bdss_client_render.R @ 0:1cc0ed4567e1 draft default tip
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_bdss_client_main commit d9ab791a7ce12362dc6e28c0a518a3f23dd581fe-dirty
author | mingchen0919 |
---|---|
date | Tue, 17 Oct 2017 14:07:18 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1cc0ed4567e1 |
---|---|
1 library(getopt) | |
2 library(rmarkdown) | |
3 library(htmltools) | |
4 library(dplyr) | |
5 library(RCurl) | |
6 | |
7 | |
8 ##============ Sink warnings and errors to a file ============== | |
9 ## use the sink() function to wrap all code within it. | |
10 ##============================================================== | |
11 zz = file('warnings_and_errors.txt') | |
12 sink(zz) | |
13 sink(zz, type = 'message') | |
14 ##---------below is the code for rendering .Rmd templates----- | |
15 | |
16 ##=============STEP 1: handle command line arguments========== | |
17 ## | |
18 ##============================================================ | |
19 # column 1: the long flag name | |
20 # column 2: the short flag alias. A SINGLE character string | |
21 # column 3: argument mask | |
22 # 0: no argument | |
23 # 1: argument required | |
24 # 2: argument is optional | |
25 # column 4: date type to which the flag's argument shall be cast. | |
26 # possible values: logical, integer, double, complex, character. | |
27 #------------------------------------------------------------- | |
28 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ | |
29 # 1. short flag alias should match the flag in the command section in the XML file. | |
30 # 2. long flag name can be any legal R variable names | |
31 # 3. two names in args_list can have common string but one name should not be a part of another name. | |
32 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. | |
33 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
34 ##------- 1. input data --------------------- | |
35 args_list=list() | |
36 args_list$URLS = c('urls', 'i', '1', 'character') | |
37 args_list$ECHO = c('echo', 'e', '1', 'character') | |
38 ##--------2. output report and outputs -------------- | |
39 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character') | |
40 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') | |
41 args_list$SINK_OUTPUT = c('sink_message', 's', '1', 'character') | |
42 ##--------3. Rmd templates in the tool directory ---------- | |
43 args_list$BDSS_CLIENT_RMD = c('bdss_client_rmd', 't', '1', 'character') | |
44 | |
45 opt = getopt(t(as.data.frame(args_list))) | |
46 | |
47 | |
48 ##=======STEP 2: create report directory (optional)========== | |
49 ## | |
50 ##=========================================================== | |
51 dir.create(opt$report_dir) | |
52 | |
53 ##=STEP 3: replace placeholders in .Rmd with argument values= | |
54 ## | |
55 ##=========================================================== | |
56 #++ need to replace placeholders with args values one by one+ | |
57 #----- 01 bdss_client.Rmd ----------------------- | |
58 readLines(opt$bdss_client_rmd) %>% | |
59 (function(x) { | |
60 gsub('URLS', opt$urls, x) | |
61 }) %>% | |
62 (function(x) { | |
63 gsub('ECHO', opt$echo, x) | |
64 }) %>% | |
65 (function(x) { | |
66 gsub('REPORT_DIR', opt$report_dir, x) | |
67 }) %>% | |
68 (function(x) { | |
69 fileConn = file('bdss_client.Rmd') | |
70 writeLines(x, con=fileConn) | |
71 close(fileConn) | |
72 }) | |
73 | |
74 ##=============STEP 4: render .Rmd templates================= | |
75 ## | |
76 ##=========================================================== | |
77 render('bdss_client.Rmd', output_file = opt$report_html) | |
78 | |
79 | |
80 ##--------end of code rendering .Rmd templates---------------- | |
81 sink() | |
82 ##=========== End of sinking output============================= |