-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwatchout.R
177 lines (151 loc) · 4.25 KB
/
watchout.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
watchout <- function(handler = new_output_handler(),
new_device = TRUE,
debug = FALSE,
frame = parent.frame()) {
if (new_device) {
# Ensure we have a graphics device available for recording, but choose
# one that's available on all platforms and doesn't write to disk
pdf(file = NULL)
dev.control(displaylist = "enable")
dev <- dev.cur()
defer(dev.off(dev), frame)
}
# Maintain a list of outputs that we'll grow over time
output <- list()
i <- 1
push <- function(value) {
output[i] <<- list(value)
i <<- i + 1
switch(output_type(value),
plot = handler$graphics(value),
text = handler$text(value),
message = handler$message(value),
warning = handler$warning(value),
error = handler$error(value)
)
invisible()
}
push_source <- function(src, tle) {
source <- new_source(src, tle, handler$source)
if (!is.null(source)) {
push(source)
}
}
# record current devices for plot handling
last_plot <- NULL
devn <- length(dev.list())
dev <- dev.cur()
sink_con <- local_persistent_sink_connection(debug, frame)
capture_plot <- function(incomplete = FALSE) {
# only record plots for our graphics device
if (!identical(dev.cur(), dev)) {
return()
}
# current page is incomplete
if (!par("page") && !incomplete) {
return()
}
plot <- recordPlot()
if (!makes_visual_change(plot[[1]])) {
return()
}
if (!looks_different(last_plot[[1]], plot[[1]])) {
return()
}
last_plot <<- plot
push(plot)
invisible()
}
capture_output <- function() {
out <- sink_con()
if (!is.null(out)) {
push(out)
}
invisible()
}
capture_plot_and_output <- function() {
capture_plot()
capture_output()
}
print_value <- function(value, visible, envir) {
if (!show_value(handler, visible)) {
return()
}
pv <- withVisible(handle_value(handler, value, visible, envir))
capture_plot_and_output()
# If the return value is visible, save the value to the output
if (pv$visible) {
push(pv$value)
}
}
check_devices <- function() {
# if dev.off() was called, make sure to restore device to the one opened
# when watchout() was called
if (length(dev.list()) < devn) {
dev.set(dev)
}
devn <<- length(dev.list())
invisible()
}
local_console_flusher(capture_output, frame = frame)
local_plot_hooks(capture_plot_and_output, frame = frame)
list(
capture_plot = capture_plot,
capture_output = capture_output,
capture_plot_and_output = capture_plot_and_output,
check_devices = check_devices,
push = push,
push_source = push_source,
print_value = print_value,
get = function() new_evaluation(output)
)
}
# Persistent way to capture output ---------------------------------------------
local_persistent_sink_connection <- function(debug = FALSE,
frame = parent.frame()) {
con <- file("", "w+b")
defer(if (isValid(con)) close(con), frame)
# try() defaults to using stderr() so we need to explicitly override(#88)
old <- options(try.outFile = con)
defer(options(old), frame)
sink(con, split = debug)
sinkn <- sink.number()
defer(if (sink.number() >= sinkn) sink(), frame)
function() {
if (!isValid(con)) {
con <<- file("", "w+b")
options(try.outFile = con)
}
if (sink.number() < sinkn) {
sink(con)
sinkn <<- sink.number()
}
read_con(con)
}
}
read_con <- function(con, buffer = 32 * 1024) {
bytes <- raw()
repeat {
new <- readBin(con, "raw", n = buffer)
if (length(new) == 0) break
bytes <- c(bytes, new)
}
if (length(bytes) == 0) {
NULL
} else {
rawToChar(bytes)
}
}
# isOpen doesn't work for two reasons:
# 1. It errors if con has been closed, rather than returning FALSE
# 2. If returns TRUE if con has been closed and a new connection opened
#
# So instead we retrieve the connection from its number and compare to the
# original connection. This works because connections have an undocumented
# external pointer.
isValid <- function(con) {
tryCatch(
identical(getConnection(con), con),
error = function(cnd) FALSE
)
}