Mercurial > repos > mingchen0919 > rmarkdown_bdss_client
comparison bdss_client_render.R @ 17:246cb7315673 draft
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_bdss_client commit 65f2ce692f3c46ccea55ee47f3c2cb2f8a2bb36f-dirty
author | mingchen0919 |
---|---|
date | Sat, 14 Oct 2017 12:12:10 -0400 |
parents | 072568c2c96a |
children | 531c00a2acbf |
comparison
equal
deleted
inserted
replaced
16:c2150a0f9c7c | 17:246cb7315673 |
---|---|
1 ##======= Handle arguments from command line ======== | 1 ##============ Sink warnings and errors to a file ============== |
2 # setup R error handline to go to stderr | 2 ## use the sink() function to wrap all code within it. |
3 options(show.error.messages=FALSE, | 3 ##============================================================== |
4 error=function(){ | |
5 cat(geterrmessage(), file=stderr()) | |
6 quit("no", 1, F) | |
7 }) | |
8 | |
9 # we need that to not crash galaxy with an UTF8 error on German LC settings. | |
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
11 | |
12 # suppress warning | |
13 options(warn = -1) | |
14 | |
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE) | |
16 args = commandArgs(trailingOnly=TRUE) | |
17 | |
18 suppressPackageStartupMessages({ | |
19 library(getopt) | |
20 library(tools) | |
21 }) | |
22 | |
23 #/////////////////////// SINK WARNINGS AND ERRORS TO A FILE FOR DEBUGGING /////////// | |
24 zz = file('warnings_and_errors.txt') | 4 zz = file('warnings_and_errors.txt') |
25 sink(zz) | 5 sink(zz) |
26 sink(zz, type = 'message') | 6 sink(zz, type = 'message') |
7 ##---------below is the code for rendering .Rmd templates----- | |
27 | 8 |
28 # column 1: the long flag name | 9 ##=============STEP 1: handle command line arguments========== |
29 # column 2: the short flag alias. A SINGLE character string | 10 ## |
30 # column 3: argument mask | 11 ##============================================================ |
31 # 0: no argument | 12 # column 1: the long flag name |
32 # 1: argument required | 13 # column 2: the short flag alias. A SINGLE character string |
33 # 2: argument is optional | 14 # column 3: argument mask |
34 # column 4: date type to which the flag's argument shall be cast. | 15 # 0: no argument |
35 # possible values: logical, integer, double, complex, character. | 16 # 1: argument required |
36 ##------- 1. input data --------------------- | 17 # 2: argument is optional |
37 spec_list=list() | 18 # column 4: date type to which the flag's argument shall be cast. |
38 spec_list$URLS = c('urls', 'i', '1', 'character') | 19 # possible values: logical, integer, double, complex, character. |
39 spec_list$ECHO = c('echo', 'e', '1', 'character') | 20 #------------------------------------------------------------- |
40 ##--------2. output report and outputs -------------- | 21 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ |
41 spec_list$REPORT_HTML = c('report_html', 'r', '1', 'character') | 22 # 1. short flag alias should match the flag in the command section in the XML file. |
42 spec_list$OUTPUT_DIR = c('output_dir', 'd', '1', 'character') | 23 # 2. long flag name can be any legal R variable names |
43 spec_list$SINK_OUTPUT = c('sink_message', 's', '1', 'character') | 24 # 3. two names in args_list can have common string but one name should not be a part of another name. |
44 ##--------3. Rmd templates in the tool directory ---------- | 25 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. |
45 spec_list$bdss_client_sra_se_RMD = c('bdss_client_sra_se_rmd', 't', '1', 'character') | 26 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
46 | 27 ##------- 1. input data --------------------- |
47 spec = t(as.data.frame(spec_list)) | 28 args_list=list() |
48 opt = getopt(spec) | 29 args_list$URLS = c('urls', 'i', '1', 'character') |
49 | 30 args_list$ECHO = c('echo', 'e', '1', 'character') |
50 #------ Load libraries --------- | 31 ##--------2. output report and outputs -------------- |
51 library(rmarkdown) | 32 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character') |
52 library(htmltools) | 33 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') |
53 library(dplyr) | 34 args_list$SINK_OUTPUT = c('sink_message', 's', '1', 'character') |
54 library(RCurl) | 35 ##--------3. Rmd templates in the tool directory ---------- |
55 | 36 args_list$BDSS_CLIENT_RMD = c('bdss_client_rmd', 't', '1', 'character') |
56 #----- 1. create the report directory ------------------------ | 37 |
57 dir.create(opt$output_dir) | 38 opt = getopt(t(as.data.frame(args_list))) |
58 | 39 |
59 #----- 2. generate Rmd files with Rmd templates -------------- | 40 ##=============STEP 2: loading libraries===================== |
60 # a. templates without placeholder variables: | 41 ## |
61 # copy templates from tool directory to the working directory. | 42 ##=========================================================== |
62 # b. templates with placeholder variables: | 43 library(rmarkdown) |
63 # substitute variables with user input values and place them in the working directory. | 44 library(htmltools) |
64 | 45 library(dplyr) |
65 #----- 01 bdss_client_sra_se.Rmd ----------------------- | 46 library(RCurl) |
66 readLines(opt$bdss_client_sra_se_rmd) %>% | 47 |
67 (function(x) { | 48 ##=======STEP 3: create report directory (optional)========== |
68 gsub('URLS', opt$urls, x) | 49 ## |
69 }) %>% | 50 ##=========================================================== |
70 (function(x) { | 51 dir.create(opt$report_dir) |
71 gsub('ECHO', opt$echo, x) | 52 |
72 }) %>% | 53 ##=STEP 4: replace placeholders in .Rmd with argument values= |
73 (function(x) { | 54 ## |
74 gsub('OUTPUT_DIR', opt$output_dir, x) | 55 ##=========================================================== |
75 }) %>% | 56 #++ need to replace placeholders with args values one by one+ |
76 (function(x) { | 57 #----- 01 bdss_client.Rmd ----------------------- |
77 fileConn = file('bdss_client_sra_se.Rmd') | 58 readLines(opt$bdss_client_rmd) %>% |
78 writeLines(x, con=fileConn) | 59 (function(x) { |
79 close(fileConn) | 60 gsub('URLS', opt$urls, x) |
80 }) | 61 }) %>% |
81 | 62 (function(x) { |
82 #------ 3. render all Rmd files -------- | 63 gsub('ECHO', opt$echo, x) |
83 render('bdss_client_sra_se.Rmd', output_file = opt$report_html) | 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 5: render .Rmd templates================= | |
75 ## | |
76 ##=========================================================== | |
77 render('bdss_client.Rmd', output_file = opt$report_html) | |
84 | 78 |
85 | 79 |
86 #-------4. manipulate outputs ----------------------------- | 80 ##--------end of code rendering .Rmd templates---------------- |
87 | |
88 | |
89 | |
90 | |
91 | |
92 sink() | 81 sink() |
93 #/////////// END OF SINK OUTPUT /////////////////////////// | 82 ##=========== End of sinking output============================= |