comparison w4mcorcov_util.R @ 0:23f9fad4edfc draft

planemo upload for repository https://github.com/HegemanLab/w4mcorcov_galaxy_wrapper/tree/master commit bd26542b811de06c1a877337a2840a9f899c2b94
author eschen42
date Mon, 16 Oct 2017 14:56:52 -0400
parents
children 50f60f94c034
comparison
equal deleted inserted replaced
-1:000000000000 0:23f9fad4edfc
1 # tryCatchFunc wraps an expression that produces a value if it does not stop:
2 # tryCatchFunc produces a list
3 # On success of expr(), tryCatchFunc produces
4 # list(success TRUE, value = expr(), msg = "")
5 # On failure of expr(), tryCatchFunc produces
6 # list(success = FALSE, value = NA, msg = "the error message")
7 tryCatchFunc <- function(expr) {
8 # format error for logging
9 format_error <- function(e) {
10 paste(c("Error { message:", e$message, ", call:", e$call, "}"), collapse = " ")
11 }
12 retval <- NULL
13 tryCatch(
14 expr = {
15 retval <- ( list( success = TRUE, value = eval(expr = expr), msg = "" ) )
16 }
17 , error = function(e) {
18 retval <<- list( success = FALSE, value = NA, msg = format_error(e) )
19 }
20 )
21 return (retval)
22 }
23
24 # turn off all plotting devices
25 dev.off.all <- function() {
26 while (!is.null(dev.list())) { dev.off() }
27 }
28
29 # capture plot and write to PDF; then close any devices opened in the process
30 plot2pdf <- function(
31 file.name
32 , plot.function
33 , width = 12
34 , height = 12
35 ) {
36 # capture plot and write to PDF
37 cur.dev <- dev.list()
38 filename <- file.name
39 pdf(file = filename, width = width, height = height)
40 plot.function()
41 # close any devices opened in the process
42 dev.off()
43 if (is.null(cur.dev)) {
44 dev.off.all()
45 } else {
46 while ( length(dev.list()) > length(cur.dev) ) { dev.off() }
47 }
48 }
49
50 # print and capture plot and write to PDF; then close any devices opened in the process
51 # This is needed for ggplot which does not print the plot when invoked within a function.
52 print2pdf <- function(
53 file.name
54 , plot.function
55 , width = 12
56 , height = 12
57 ) {
58 plot2pdf(
59 file.name = file.name
60 , width = width
61 , height = height
62 , plot.function = function() {
63 print(plot.function())
64 }
65 )
66 }
67
68 iso8601.znow <- function()
69 {
70 strftime(as.POSIXlt(Sys.time(), "UTC"), "%Y-%m-%dT%H:%M:%SZ")
71 }
72
73 # pdf.name <- function(name)
74 # {
75 # paste0(name, "_", iso8601.filename.fragment(), ".pdf")
76 # }
77 #
78 # tsv.name <- function(name)
79 # {
80 # paste0(name, "_", iso8601.filename.fragment(), ".tsv")
81 # }
82 #
83 # # pseudo-inverse - computational inverse non-square matrix a
84 # p.i <- function(a) {
85 # solve(t(a) %*% a) %*% t(a)
86 # }
87
88