Apply a step to data analysis
add_log_info.Rd
add_log_info()
encapsulates an analysis performed by another function, to
feed a consistently saved log. Messages can also be returned directly.
Arguments
- x
data to be adapted.
- fun
function, the analysis that may performed on x.
- info
list, gives the name of the step both in 'short' and 'long' terms.
- journal
character or FALSE. See details.
- verbose
logical, should the some messages be sent in real time.
- verification
expression, a simple treatment that will be added to *log.csv, may be missing.
- level
integer, the number of tabulation that will be added to the message.
- ...
anything that may be needed by fun.
Details
add_log_info()
is designed to feed a log step by step during a
multi-stage analysis:
Information on stage names is entered in the
info
parameter, in both short (for the log) and long (for on-the-fly messages) versions. -The level of analysis is controlled by thelevel
parameter, which influences the number of tabs in messages sent or saved.
Messages generated include step names and elapsed time for analysis. These messages can be completed in two ways:
An expression can be sent via the
verification
parameter, which must be kept simple and act either on input data (x) or on results (result).messages returned by the
fun
function are captured and minimally formatted by adding tabs according to the level value.
Captured messages are added to the log file, while those generated by
verification
are added to a dedicated csv file.
WARNING: because of partial match used in R for arguments, some errors may occur when add_log_info is used without naming explicitly arguments.
Examples
journal = file.path(tempdir(), "journal.log")
info = list("short" = "Mean by Species", "long" = "Mean by Species")
result = add_log_info(
iris[4],
aggregate,
info,
journal,
by = iris["Species"],
FUN = mean,
verification = expression(ncol(result))
)
#> - Mean by Species...
#> done (0 secs)
aggregate_special = function(x, ...) {
selection = sapply(x, is.numeric) | sapply(x, is.logical)
if (sum(!selection) != 0) {
message(
sprintf(
"- %s colum(s) are(is) neither numeric nor logical (%s). ",
sum(!selection),
paste0(names(x)[!selection], "|")
),
"They are(It is) dropped."
)
}
aggregate(x[selection], ...)
}
result = add_log_info(
iris,
aggregate_special,
info,
journal,
by = iris["Species"],
FUN = mean,
verification = expression(ncol(result))
)
#> - Mean by Species...
#> - 1 colum(s) are(is) neither numeric nor logical (Species|). They are(It is) dropped.
#> done (0 secs)