comparison rmarkdown_report_render.R @ 0:a42e58c71e5b draft default tip

planemo upload commit 841d8b22bf9f1aaed6bfe8344b60617f45b275b2-dirty
author mingchen0919
date Sun, 30 Dec 2018 12:55:49 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a42e58c71e5b
1 ##============ Sink warnings and errors to a file ==============
2 ## use the sink() function to wrap all code within it.
3 ##==============================================================
4 zz = file(paste0(Sys.getenv('REPORT_FILES_PATH'), '/.r_rendering.log.txt'))
5 sink(zz)
6 sink(zz, type = 'message')
7
8 #============== preparation ====================================
9 # import libraries
10 #------------------------------------------------------------------
11 # ADD MORE LIBRARIES HERE IF YOUR TOOL DEPENDS ON OTHER R LIBRARIES
12 #------------------------------------------------------------------
13 library('getopt')
14 library('rmarkdown')
15 library('htmltools')
16 #------------------------------------------------------------------
17 options(stringsAsFactors = FALSE)
18
19
20 # define two helper functions
21 #-----: helper function 1
22 #' \code{getopt_specification_matrix} returns a getopt specification matrix.
23 #'
24 #' @param specification_file a cvs file within the \code{galaxy_tool_directory} which stores getopt specification matrix data.
25 #' The first column are short flags, the second column are argument masks, the third column
26 #' is data types. The fourth column are variable names used in the tool XML. These three columns are required.
27 #' @param gtg_name the name of a running GTG.
28 getopt_specification_matrix = function(specification_file,
29 gtg_name = 'gtg',
30 tool_dir = Sys.getenv('TOOL_INSTALL_DIR')) {
31 df = read.csv(
32 paste0(tool_dir, '/', specification_file),
33 header = TRUE,
34 stringsAsFactors = FALSE
35 )
36 # check if there are duplicated short flags
37 short_flags = df[, 1]
38 if (length(unique(short_flags)) < length(short_flags)) {
39 cat('----Duplicated short flags found ----\n')
40 cat('short flags: ', df[, 1][duplicated(df[, 1])], '\n')
41 stop('Duplicated short flags are not allowed.')
42 }
43
44 # use short flags to generate long flags
45 long_flags = paste0('X_', df[, 1])
46
47 # specification matrix
48 df2 = data.frame(
49 long_flags = long_flags,
50 short_flags = df[, 1],
51 argument_mask = df[, 2],
52 data_type = df[, 3]
53 )
54
55 as.matrix(df2)
56 }
57
58 #-----: helper function 2
59 #' \code{file_tree} generate file tree of a directory in the format of HTML lists.
60 #'
61 #' @param dir the path to the directory for generating the file tree.
62 #' @param output_dir the REPORT_FILES_PATH folder name, which has the name style: dataset_NUMBER_files.
63 # define a recursive function to build html string of the file tree
64 file_tree = function(dir = '.') {
65 # get the OUTPUT_DIR folder data: dataset_NUMBER_files
66 report_files_path = Sys.getenv('REPORT_FILES_PATH')
67 output_dir = tail(strsplit(report_files_path, '/')[[1]], 1)
68
69 files = list.files(path = dir,
70 recursive = FALSE,
71 full.names = TRUE)
72 # files also include directorys, need to remove directorys
73 files = files[!dir.exists(files)]
74 dirs = list.dirs(path = dir,
75 recursive = FALSE,
76 full.names = TRUE)
77 tags$ul({
78 if (length(files) > 0) {
79 lapply(files, function(x) {
80 path_end = tail(strsplit(x, '/')[[1]], 1)
81 href_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
82 li_item = tags$li(tags$a(path_end, href = href_path))
83 li_item$attribs = list('data-jstree' = '{"icon":"jstree-file"}')
84 li_item
85 })
86 }
87 },
88 {
89 if (length(dirs) > 0) {
90 lapply(dirs, function(x) {
91 path_end = tail(strsplit(x, '/')[[1]], 1)
92 # hide vakata-jstree-3.3.5 folder
93 if (!(path_end %in% c('vakata-jstree-3.3.5', 'rmarkdown_report_files', 'site_libs'))) {
94 # x_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
95 li_item = tags$li(path_end, file_tree(x))
96 li_item$attribs = list('data-jstree' = '{"icon":"jstree-folder"}')
97 li_item
98 }
99 })
100 }
101 })
102 }
103 #----------------- end of help functions -------------------------
104
105
106 # import getopt specification matrix from a csv file
107 opt = getopt(getopt_specification_matrix('command-line-arguments.csv',
108 tool_dir = Sys.getenv('TOOL_INSTALL_DIR')))
109 # define environment variables for all input values. this is useful when we
110 # want to use input values by other programming language in r markdown
111 do.call(Sys.setenv, opt[-1])
112 #===============================================================
113
114
115 #======================== render Rmd files =========================
116 # copy jstree javascript library to tool output directory
117 file.copy(
118 from = paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/vakata-jstree-3.3.5'),
119 to = Sys.getenv('REPORT_FILES_PATH'),
120 recursive = TRUE
121 )
122
123 # if '_site.yml' file exists, this tool is assumed to render a website.
124 # otherwise, it renders a single html.
125 if (file.exists(paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/_site.yml'))) {
126 # render a website
127 system(command = 'cp -r ${TOOL_INSTALL_DIR}/*.Rmd ${REPORT_FILES_PATH}')
128 system(command = 'cp -r ${TOOL_INSTALL_DIR}/_site.yml ${REPORT_FILES_PATH}')
129 render_site(input = Sys.getenv('REPORT_FILES_PATH'))
130 } else {
131 # render a single html
132 system(command = 'cp -r ${TOOL_INSTALL_DIR}/rmarkdown_report.Rmd ${REPORT_FILES_PATH}')
133 # add a few lines to 'rmarkdown_report.Rmd' to generate file tree outputs
134 jstree_lines = '
135 ## Outputs
136
137 ```{r, echo=FALSE}
138 tags$div(id="jstree", file_tree(Sys.getenv(\'REPORT_FILES_PATH\')))
139 ```'
140 write(
141 x = jstree_lines,
142 append = TRUE,
143 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd')
144 )
145 render(input = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd'))
146 }
147 #===============================================================
148
149
150 #============== expose outputs to galaxy history ===============
151 system(command = 'sh ${TOOL_INSTALL_DIR}/expose-outputs-to-galaxy-history.sh')
152 #===============================================================
153
154
155 ##--------end of code rendering .Rmd templates----------------
156 sink()
157 ##=========== End of sinking output=============================