# HG changeset patch # User guerler # Date 1396913038 14400 # Node ID 7774e0097ff42dda53656bd70ebc35860253788a # Parent 533b6c8f8584ec36901baa5c54b9842ccc6ce139 Uploaded diff -r 533b6c8f8584 -r 7774e0097ff4 chartskit.r --- 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)