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