|
1 |
| -#' Object class tests |
2 |
| -#' |
3 |
| -#' @keywords internal |
4 |
| -#' @rdname is.message |
5 |
| -#' @export |
6 |
| -is.message <- function(x) inherits(x, "message") |
7 |
| -#' @rdname is.message |
8 |
| -#' @export |
9 |
| -is.warning <- function(x) inherits(x, "warning") |
10 |
| -#' @rdname is.message |
11 |
| -#' @export |
12 |
| -is.error <- function(x) inherits(x, "error") |
13 |
| -#' @rdname is.message |
14 |
| -#' @export |
15 |
| -is.value <- function(x) inherits(x, "value") |
16 |
| -#' @rdname is.message |
17 |
| -#' @export |
18 |
| -is.source <- function(x) inherits(x, "source") |
19 |
| -#' @rdname is.message |
20 |
| -#' @export |
21 |
| -is.recordedplot <- function(x) inherits(x, "recordedplot") |
22 |
| - |
23 |
| -new_value <- function(value, visible = TRUE) { |
24 |
| - structure(list(value = value, visible = visible), class = "value") |
25 |
| -} |
26 |
| - |
27 | 1 | new_source <- function(src, call, handler = NULL) {
|
28 | 2 | src <- structure(list(src = src), class = "source")
|
29 | 3 | if (is.null(handler)) {
|
@@ -61,111 +35,3 @@ handle_value <- function(handler, value, visible) {
|
61 | 35 | }
|
62 | 36 |
|
63 | 37 | render <- function(x) if (isS4(x)) methods::show(x) else print(x)
|
64 |
| - |
65 |
| -#' Custom output handlers |
66 |
| -#' |
67 |
| -#' An `output_handler` handles the results of [evaluate()], |
68 |
| -#' including the values, graphics, conditions. Each type of output is handled by |
69 |
| -#' a particular function in the handler object. |
70 |
| -#' |
71 |
| -#' The handler functions should accept an output object as their first argument. |
72 |
| -#' The return value of the handlers is ignored, except in the case of the |
73 |
| -#' `value` handler, where a visible return value is saved in the output |
74 |
| -#' list. |
75 |
| -#' |
76 |
| -#' Calling the constructor with no arguments results in the default handler, |
77 |
| -#' which mimics the behavior of the console by printing visible values. |
78 |
| -#' |
79 |
| -#' Note that recursion is common: for example, if `value` does any |
80 |
| -#' printing, then the `text` or `graphics` handlers may be called. |
81 |
| -#' |
82 |
| -#' @param source Function to handle the echoed source code under evaluation. |
83 |
| -#' This function should take two arguments (`src` and `call`), and return |
84 |
| -#' an object that will be inserted into the evaluate outputs. `src` is the |
85 |
| -#' unparsed text of the source code, and `call` is the parsed language object |
86 |
| -#' If `src` is unparsable, `call` will be `expression()`. |
87 |
| -#' |
88 |
| -#' Return `src` for the default evaluate behaviour. Return `NULL` to |
89 |
| -#' drop the source from the output. |
90 |
| -#' @param text Function to handle any textual console output. |
91 |
| -#' @param graphics Function to handle graphics, as returned by |
92 |
| -#' [recordPlot()]. |
93 |
| -#' @param message Function to handle [message()] output. |
94 |
| -#' @param warning Function to handle [warning()] output. |
95 |
| -#' @param error Function to handle [stop()] output. |
96 |
| -#' @param value Function to handle the values returned from evaluation. |
97 |
| -#' * If it has one argument, it called on visible values. |
98 |
| -#' * If it has two arguments, it handles all values, with the second |
99 |
| -#' argument indicating whether or not the value is visible. |
100 |
| -#' @param calling_handlers List of [calling handlers][withCallingHandlers]. |
101 |
| -#' These handlers have precedence over the exiting handler installed |
102 |
| -#' by [evaluate()] when `stop_on_error` is set to 0. |
103 |
| -#' @return A new `output_handler` object |
104 |
| -#' @aliases output_handler |
105 |
| -#' @export |
106 |
| -new_output_handler <- function(source = identity, |
107 |
| - text = identity, |
108 |
| - graphics = identity, |
109 |
| - message = identity, |
110 |
| - warning = identity, |
111 |
| - error = identity, |
112 |
| - value = render, |
113 |
| - calling_handlers = list()) { |
114 |
| - source <- match.fun(source) |
115 |
| - stopifnot(length(formals(source)) >= 1) |
116 |
| - text <- match.fun(text) |
117 |
| - stopifnot(length(formals(text)) >= 1) |
118 |
| - graphics <- match.fun(graphics) |
119 |
| - stopifnot(length(formals(graphics)) >= 1) |
120 |
| - message <- match.fun(message) |
121 |
| - stopifnot(length(formals(message)) >= 1) |
122 |
| - warning <- match.fun(warning) |
123 |
| - stopifnot(length(formals(warning)) >= 1) |
124 |
| - error <- match.fun(error) |
125 |
| - stopifnot(length(formals(error)) >= 1) |
126 |
| - value <- match.fun(value) |
127 |
| - stopifnot(length(formals(value)) >= 1) |
128 |
| - |
129 |
| - check_handlers(calling_handlers) |
130 |
| - |
131 |
| - structure( |
132 |
| - list( |
133 |
| - source = source, |
134 |
| - text = text, |
135 |
| - graphics = graphics, |
136 |
| - message = message, |
137 |
| - warning = warning, |
138 |
| - error = error, |
139 |
| - value = value, |
140 |
| - calling_handlers = calling_handlers |
141 |
| - ), |
142 |
| - class = "output_handler" |
143 |
| - ) |
144 |
| -} |
145 |
| - |
146 |
| -check_handlers <- function(x) { |
147 |
| - if (!is.list(x)) { |
148 |
| - stop_bad_handlers() |
149 |
| - } |
150 |
| - |
151 |
| - if (!length(x)) { |
152 |
| - return() |
153 |
| - } |
154 |
| - |
155 |
| - names <- names(x) |
156 |
| - if (!is.character(names) || anyNA(names) || any(names == "")) { |
157 |
| - stop_bad_handlers() |
158 |
| - } |
159 |
| - |
160 |
| - for (elt in x) { |
161 |
| - if (!is.function(elt)) { |
162 |
| - stop_bad_handlers() |
163 |
| - } |
164 |
| - } |
165 |
| -} |
166 |
| -stop_bad_handlers <- function() { |
167 |
| - stop(simpleError( |
168 |
| - "`calling_handlers` must be a named list of functions.", |
169 |
| - call = call("new_output_handler") |
170 |
| - )) |
171 |
| -} |
0 commit comments