-
Notifications
You must be signed in to change notification settings - Fork 2
/
debugfunc.R
191 lines (156 loc) · 5.7 KB
/
debugfunc.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# debugfunc.R
# =============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
# =============================================================================
debugfunc <- new.env()
# =============================================================================
# Debug message
# =============================================================================
debugfunc$debug_message <- function(..., file = "", filename = "", append = TRUE) {
# Use the file parameter OR the filename parameter OR neither.
mode <- "console"
if (file != "") {
mode <- "file"
} else if (filename != "") {
mode <- "filename"
}
if (mode == "file") {
sink(file, append = append)
} else if (mode == "filename") {
sink(filename, append = append)
}
cat(..., "\n", sep = "")
if (mode == "file" || mode == "filename") {
sink()
}
}
# =============================================================================
# Debug a thing
# =============================================================================
debugfunc$debug_quantity <- function(
x, file = "", filename = "", append = TRUE,
progress_to_console = TRUE, print_only = FALSE
) {
# Use the file parameter OR the filename parameter OR neither.
# To write to file, can use:
# cat(..., file = file)
# dput(x, file = file)
# but not:
# print(x)
# So the simplest way is to use sink().
# This maintains a stack of diversions, so we divert then un-divert
# Note also:
# f <- open("output.txt", open = "w") # w = write, a = append
# # write stuff
# flush(f) # if desired
# close(f)
mode <- "console"
if (file != "") {
mode <- "file"
} else if (filename != "") {
mode <- "filename"
}
x_name <- deparse(substitute(x)) # fetch the variable name passed in
LINEBREAK_1 <- paste(c(rep("=", 79), "\n"), collapse = "")
LINEBREAK_2 <- paste(c(rep("-", 79), "\n"), collapse = "")
if (progress_to_console) {
destination <- mode
if (mode == "filename") {
destination <- filename
}
cat("DEBUGGING QUANTITY: ", x_name, " TO ", destination, "\n", sep = "")
}
if (mode == "file") {
sink(file, append = append)
} else if (mode == "filename") {
sink(filename, append = append)
}
cat(LINEBREAK_1, "DEBUGGING QUANTITY: ", x_name, "\n", sep = "")
cat(LINEBREAK_2, "... print(", x_name, "):\n", LINEBREAK_2, sep = "")
print(x)
if (!print_only) {
cat(LINEBREAK_2, "... dput(", x_name, "):\n", LINEBREAK_2, sep = "")
dput(x, file = file)
}
cat(LINEBREAK_1, file = file, sep = "")
if (mode == "file" || mode == "filename") {
sink()
}
}
#f <- file("output.txt", open = "w")
#x <- list(a = 1, b = 2)
#debug_quantity(x, file = f)
#close(f)
debugfunc$wtf_is <- function(x) {
# For when you have no idea what something is.
# http://stackoverflow.com/questions/8855589
cat("1. typeof():\n")
print(typeof(x))
cat("\n2. class():\n")
print(class(x))
cat("\n3. mode():\n")
print(mode(x))
cat("\n4. names() [try x$somename, x[\"somename\"], x[[\"somename\"]] ]:\n")
print(names(x))
cat("\n5. slotNames() [use x@someslot or slot(x, \"someslot\")]:\n")
print(slotNames(x))
cat("\n6. attributes() [use attr(x, \"someattr\")]:\n")
print(attributes(x))
cat("\n7. str():\n")
print(str(x))
cat("\n8. methods, for each class type:\n")
for (c in class(x)) {
cat(paste('\n- methods(class = "', c, '"):\n', sep = ""))
print(methods(class = c))
}
}
# =============================================================================
# Output columns
# =============================================================================
debugfunc$wideScreen <- function(howWide = Sys.getenv("COLUMNS")) {
if (nchar(howWide) > 0) { # under non-console clusters, envvar can be empty
options(width = as.integer(howWide))
}
}
# =============================================================================
# Nested status messages
# =============================================================================
debugfunc$STATUS_DISPLAY_LEVEL <- 0
debugfunc$status <- function(msg, ellipsis = FALSE, increment = FALSE, colour = "blue") {
cat(
style(
paste(
paste(rep(" ", debugfunc$STATUS_DISPLAY_LEVEL), collapse = ""),
msg,
ifelse(ellipsis, "...", ""),
"\n",
sep = ""
),
fg = colour
)
)
if (increment) {
debugfunc$STATUS_DISPLAY_LEVEL <<- debugfunc$STATUS_DISPLAY_LEVEL + 1
}
}
debugfunc$status_start <- function(msg, ellipsis = TRUE, colour = "red") {
status(msg, increment = TRUE, ellipsis = ellipsis, colour = colour)
}
debugfunc$status_end <- function(msg = "... done", announce = TRUE, colour = "green") {
debugfunc$STATUS_DISPLAY_LEVEL <<- debugfunc$STATUS_DISPLAY_LEVEL - 1
if (announce) {
cat(style(
paste(
paste(rep(" ", debugfunc$STATUS_DISPLAY_LEVEL), collapse = ""),
msg,
"\n",
sep = ""
),
fg = colour))
}
}
# =============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
# =============================================================================
if ("debugfunc" %in% search()) detach("debugfunc")
attach(debugfunc) # subsequent additions not found, so attach at the end