Debian Codegraph @

man/record.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/record.R
3 \name{record}
4 \alias{record}
5 \alias{format.xfun_record_results}
6 \alias{print.xfun_record_results}
7 \title{Run R code and record the results}
8 \usage{
9 record(
10 code = NULL,
11 dev = "png",
12 dev.path = "xfun-record",
13 dev.ext = dev_ext(dev),
14 dev.args = list(),
15 dev.keep = TRUE,
16 message = TRUE,
17 warning = TRUE,
18 error = NA,
19 cache = list(),
20 print = record_print,
21 print.args = list(),
22 verbose = getOption("xfun.record.verbose", 0),
23 envir = parent.frame()
24 )
25
26 \method{format}{xfun_record_results}(
27 x,
28 to = c("text", "markdown", "html"),
29 encode = FALSE,
30 template = FALSE,
31 ...
32 )
33
34 \method{print}{xfun_record_results}(
35 x,
36 browse = interactive(),
37 to = if (browse) "html" else "text",
38 template = TRUE,
39 ...
40 )
41 }
42 \arguments{
43 \item{code}{A character vector of R source code.}
44
45 \item{dev}{A graphics device. It can be a function name, a function, or a
46 character string that can be evaluated to a function to open a graphics
47 device.}
48
49 \item{dev.path}{A base file path for plots. Actual plot filenames will be
50 this base path plus incremental suffixes. For example, if \code{dev.path = "foo"}, the plot files will be \verb{foo-1.png}, \verb{foo-2.png}, and so on. If
51 \code{dev.path} is not character (e.g., \code{FALSE}), plots will not be recorded.}
52
53 \item{dev.ext}{The file extension for plot files. By default, it will be
54 inferred from the first argument of the device function if possible.}
55
56 \item{dev.args}{Extra arguments to be passed to the device. The default
57 arguments are \code{list(units = 'in', onefile = FALSE, width = 7, height = 7, res = 96)}. If any of these arguments is not present in the device
58 function, it will be dropped.}
59
60 \item{dev.keep}{Indices of plots to be kept. The indices can be either
61 numeric (positive or negative integers) or logical. Negative integers and
62 false values will remove the corresponding plots. For example, if the code
63 generated 3 plots, \code{dev.keep = c(1, 3)}, \code{-2}, and \code{c(T, F, T)} are
64 equivalent ways to remove the second plot. The special value \code{dev.keep = 'last'} means to keep the last plot only.}
65
66 \item{message, warning, error}{If \code{TRUE}, record and store messages / warnings
67 / errors in the output. If \code{FALSE}, suppress them. If \code{NA}, do not process
68 them (messages will be emitted to the console, and errors will halt the
69 execution).}
70
71 \item{cache}{A list of options for caching. See the \code{path}, \code{id}, and \code{...}
72 arguments of \code{\link[=cache_exec]{cache_exec()}}.}
73
74 \item{print}{A (typically S3) function that takes the value of an expression
75 in the code as input and returns output. The default is \code{\link[=record_print]{record_print()}}.
76 If a non-function value (e.g., \code{NA}) is passed to this argument, \code{\link[=print]{print()}}
77 (or \code{\link[=show]{show()}} for S4 objects) will be used.}
78
79 \item{print.args}{A list of arguments for the \code{print} function. By default,
80 the whole list is not passed directly to the function, but only an element
81 in the list with a name identical to the first class name of the returned
82 value of the expression, e.g., \code{list(data.frame = list(digits = 3), matrix = list())}. This makes it possible to apply different print arguments to
83 objects of different classes. If the whole list is intended to be passed to
84 the print function directly, wrap the list in \code{\link[=I]{I()}}.}
85
86 \item{verbose}{\code{2} means to always print the value of each expression in the
87 code, no matter if the value is \code{\link[=invisible]{invisible()}} or not; \code{1} means to always
88 print the value of the last expression; \code{0} means no special handling
89 (i.e., print only when the value is visible).}
90
91 \item{envir}{An environment in which the code is evaluated.}
92
93 \item{x}{An object returned by \code{record()}.}
94
95 \item{to}{The output format (text, markdown, or html).}
96
97 \item{encode}{For HTML output, whether to base64 encode plots.}
98
99 \item{template}{For HTML output, whether to embed the formatted results in an
100 HTML template. Alternatively, this argument can take a file path, i.e.,
101 path to an HTML template that contains the variable \verb{$body$}. If \code{TRUE},
102 the default template in this package will be used
103 (\code{xfun:::pkg_file('resources', 'record.html')}).}
104
105 \item{...}{Currently ignored.}
106
107 \item{browse}{Whether to browse the results on an HTML page.}
108 }
109 \value{
110 \code{record()} returns a list of the class \code{xfun_record_results} that
111 contains elements with these possible classes: \code{record_source} (source
112 code), \code{record_output} (text output), \code{record_plot} (plot file paths),
113 \code{record_message} (messages), \code{record_warning} (warnings), and
114 \code{record_error} (errors, only when the argument \code{error = TRUE}).
115
116 The \code{format()} method returns a character vector of plain-text output
117 or HTML code for displaying the results.
118
119 The \code{print()} method prints the results as plain text or HTML to the
120 console or displays the HTML page.
121 }
122 \description{
123 Run R code and capture various types of output, including text output, plots,
124 messages, warnings, and errors.
125 }
126 \examples{
127 code = c("# a warning test", "1:2 + 1:3", "par(mar = c(4, 4, 1, .2))",
128 "barplot(5:1, col = 2:6, horiz = TRUE)", "head(iris)",
129 "sunflowerplot(iris[, 3:4], seg.col = 'purple')",
130 "if (TRUE) {\n message('Hello, xfun::record()!')\n}",
131 "# throw an error", "1 + 'a'")
132 res = xfun::record(code, dev.args = list(width = 9, height = 6.75),
133 error = TRUE)
134 xfun::tree(res)
135 format(res)
136 # find and clean up plot files
137 plots = Filter(function(x) inherits(x, "record_plot"),
138 res)
139 file.remove(unlist(plots))
140 }