diff w4mcorcov_util.R @ 6:0b49916c5c52 draft

planemo upload for repository https://github.com/HegemanLab/w4mcorcov_galaxy_wrapper/tree/master commit 4428e3252d54c8a8e0e5d85e8eaaeb13e9b21de7
author eschen42
date Wed, 05 Sep 2018 19:24:47 -0400
parents 50a07adddfbd
children
line wrap: on
line diff
--- a/w4mcorcov_util.R	Fri Mar 02 08:26:36 2018 -0500
+++ b/w4mcorcov_util.R	Wed Sep 05 19:24:47 2018 -0400
@@ -21,68 +21,54 @@
   return (retval)
 }
 
-# turn off all plotting devices
-dev.off.all <- function() {
-  while (!is.null(dev.list())) { dev.off() }
+errorSink <- function(which_function, ...) {
+  var_args <- "..."
+  tryCatch(
+    var_args <<- (deparse(..., width.cutoff = 60))
+  , error = function(e) {print(e$message)}
+  )
+  if (var_args == "...")
+    return
+  # format error for logging
+  format_error <- function(e) {
+    sprintf(
+      "Error\n{  message: %s\n, arguments: %s\n}\n"
+    , e$message
+    , Reduce(f = paste, x = var_args)
+    )
+  }
+  format_warning <- function(e) {
+    sprintf(
+      "Warning\n{  message: %s\n, arguments: %s\n}\n"
+    , e$message
+    , Reduce(f = paste, x = var_args)
+    )
+  }
+  sink_number <- sink.number()
+  sink(stderr())
+  tryCatch(
+    var_args <- (deparse(..., width.cutoff = 60))
+  , expr = {
+      retval <- which_function(...)
+    }
+    , error = function(e) cat(format_error(e), file = stderr())
+    , warning = function(w) cat(format_warning(w), file = stderr())
+  )
+  while (sink.number() > sink_number) {
+    sink()
+  }
 }
-  
-# capture plot and write to PDF; then close any devices opened in the process
-plot2pdf <- function(
-  file.name
-, plot.function
-, width = 12
-, height = 12
-) {
-  # capture plot and write to PDF
-  cur.dev <- dev.list()
-  filename <- file.name
-  pdf(file = filename, width = width, height = height)
-  plot.function()
-  # close any devices opened in the process
-  dev.off()
-  if (is.null(cur.dev)) {
-      dev.off.all()
-  } else {
-      while ( length(dev.list()) > length(cur.dev) ) { dev.off() }
-  }
+errorPrint <- function(...) {
+  errorSink(which_function = print, ...)
+}
+errorCat <- function(...) {
+  errorSink(which_function = cat, ..., "\n")
 }
 
-# print and capture plot and write to PDF; then close any devices opened in the process
-#   This is needed for ggplot which does not print the plot when invoked within a function.
-print2pdf <- function(
-  file.name
-, plot.function
-, width = 12
-, height = 12
-) {
-  plot2pdf(
-    file.name = file.name
-  , width = width
-  , height = height
-  , plot.function = function() {
-      print(plot.function())
-    }
-  )
-}
 
-iso8601.znow <- function()
-{
-  strftime(as.POSIXlt(Sys.time(), "UTC"), "%Y-%m-%dT%H:%M:%SZ")
-}
-
-# pdf.name <- function(name)
-# {
-#   paste0(name, "_", iso8601.filename.fragment(), ".pdf")
-# }
-#
-# tsv.name <- function(name)
-# {
-#   paste0(name, "_", iso8601.filename.fragment(), ".tsv")
-# }
-#
-# # pseudo-inverse - computational inverse non-square matrix a
+# # pseudo-inverse - computational inverse of non-square matrix a
 # p.i <- function(a) {
 #   solve(t(a) %*% a) %*% t(a)
 # } 
 
-
+# vim: sw=2 ts=2 et ai :