R/record_io.R
recorded_io.Rd
Record and report input and output files used and created in R scripts
record_input(call, quiet = FALSE, invisible = FALSE)
record_output(call, quiet = FALSE)
recorded_io()
clear_recorded_io()
get_recorded_io(path = NULL)
a function call to read a data file or create a new file;
alternatively, call
can be one or more file paths.
a logical value indicating whether messages should be printed
(FALSE
; default) or not (TRUE
).
a logical value indicating whether the function should
return a value (FALSE
; default) or not (TRUE
). This is mostly
useful with source
calls
a file path to an R, Rmd, qmd, or log file. Defaults to the path of the source editor context.
record_input()
returns the output of the evaluated call
.
record_output()
invisibly returns NULL
after evaluating
call
which is expected to create one or more files as a side-effect of
the call.
recorded_io()
and clear_recorded_io()
both invisibly return
NULL
.
get_recorded_io()
returns a list with two character vector elements:
input_files and output_files.
Calling either record_input()
or record_output()
will cause
information about file paths to be stored in a dedicated environment. If
call
is a function call, both functions expect attempts to detect file
names and paths based upon known arguments of common functions that read or
creates files; if the argument of the call
are not recognized, both
record_input()
and record_output()
will assume that the 1st
argument call
provided the path of the file to be recorded.
By default, a message will be printed by both record_input()
and
record_output()
to report the path of the files that were stored.
Ultimately, the collected information can be summarized by calling
recorded_io()
typically at the end of an R script. In an .Rmd file,
recorded_io()
would need to be called inside a chunk set with the
message = FALSE
option so the information could be printed to console
or the .Rout file when .Rmd file is rendered using
render
.
In some cases (eg, in an interactive R session), it could be useful to call
clear_recorded_io()
to erase all the recorded input and output file
paths.
Retrieve collected input and output files from the log of an executed R or
Rmd file with get_recorded_io()
.
if (FALSE) { # \dontrun{
df <- record_input(
read.csv(
file = '/path/to/existing/file.csv',
header = TRUE
)
)
df <- record_input(
haven::read_sas('/path/to/existing/file.sas7bdat')
)
record_input('../../some/adhoc/file.RDS')
# Information will not be recorded if the call fails
df <- record_input(
read.csv(
file = '/path/to/invalid/file.csv',
header = TRUE
)
)
record_output(
png('Rplots_%03d.png')
)
plot(1) ; plot(2)
dev.off()
recorded_io()
} # }