diff chartskit.r @ 31:7774e0097ff4 draft

Uploaded
author guerler
date Mon, 07 Apr 2014 19:23:58 -0400
parents 4aeb334de0e3
children 0197da753d1e
line wrap: on
line diff
--- a/chartskit.r	Mon Apr 07 19:23:42 2014 -0400
+++ b/chartskit.r	Mon Apr 07 19:23:58 2014 -0400
@@ -377,6 +377,7 @@
 
 # get options, using the spec as defined by the enclosed list.
 spec = matrix(c(
+    'workdir',  'w', 1, 'character', 'Work directory',
     'module',   'm', 1, 'character', 'Module name',
     'input',    'i', 1, 'character', 'Input tabular file',
     'columns',  'c', 1, 'character', 'Columns string',
@@ -388,8 +389,12 @@
 opt = getopt(spec);
 
 # show help
-if ( !is.null(opt$help) || is.null(opt$module) || is.null(opt$input) || is.null(opt$columns) || is.null(opt$output)) {
-    cat(getopt(spec, usage=TRUE));
+if ( !is.null(opt$help) ||
+    is.null(opt$module) ||
+    is.null(opt$input) ||
+    is.null(opt$columns) ||
+    is.null(opt$output)) {
+    cat(getopt(spec, usage=TRUE))
     q(status=1);
 }
 
@@ -400,20 +405,24 @@
 # read table
 table <- read.table(opt$input)
 
+# identify module file
+module_file = paste(opt$workdir, opt$module, '.r', sep='')
+
 # source module
-source(opt$module)
+source(module_file)
 
 # run module
 l = wrapper (table, columns, settings)
 
+# header
+header_title <- '# title\t\tCharts Toolkit (chartskit)'
+header_date <- paste('# date\t\t', Sys.time(), sep='')
+header_module <- paste('# module\t', opt$module, sep='')
+header_settings <- paste('# settings\t', opt$settings, sep='')
+header_columns <- paste('# columns\t', opt$columns, sep='')
+
 # fill gaps
 if (length(l) > 0) {
-    n <- max(sapply(l, length))
-    ll <- lapply(l, function(X) {
-        c(as.character(X), rep("", times = n - length(X)))
-    })
-    out <- do.call(cbind, ll)
-
     # print details
     if (!is.null(opt$verbose)) {
         print ('Columns:')
@@ -421,11 +430,26 @@
         print ('Settings:')
         print (settings)
         print ('Result:')
-        print (out)
+        print (l)
     }
 
+    # create output file
+    output <- file(opt$output, open='wt')
+    
+    # write header
+    writeLines('#', output)
+    writeLines(header_title, output)
+    writeLines(header_date, output)
+    writeLines(header_module, output)
+    writeLines(header_settings, output)
+    writeLines(header_columns, output)
+    writeLines('#', output)
+    
     # write table
-    write.table(out, file=opt$output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t')
+    write.table(l, file=output, row.names=FALSE, col.names = FALSE, quote=FALSE, sep='\t')
+    
+    # close file
+    close(output)
 } else {
     print ('Columns:')
     print (columns)