get_message
get_message.Rd
Evaluate an instruction written between "" and return the first of the error message, or the last of the warning or standard (non error non warning) messages if ever exist.
Using argument print_no = FALSE, return NULL if no message, which is convenient in some cases.
Usage
get_message(
data,
kind = "error",
header = TRUE,
print_no = FALSE,
text = NULL,
env = NULL,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
- data
Single character string.
- kind
Single character string. Either "error" to get error messages, or "warning" to get warning messages, or "message" to get non error and non warning messages.
- header
Single logical value. Add a header in the returned message?
- print_no
Single logical value. Print a message saying that no message reported?
- text
Single character string added to the output message (even if no message exists and print_no is TRUE). Inactivated if the header argument is FALSE. Write NULL if not required.
- env
An object corresponding to an existing environment. Then the data argument value is evaluated only in the indicated environment. Write NULL if not required (R scope is used). Example env = .GlobalEnv.
- safer_check
Single logical value. Perform some "safer" checks? If TRUE, checkings are performed before main code running (see https://github.com/safer-r): 1) correct lib_path argument value 2) required functions and related packages effectively present in local R lybraries and 3) R classical operators (like "<-") not overwritten by another package because of the R scope. Must be set to FALSE if this fonction is used inside another "safer" function to avoid pointless multiple checkings.
- lib_path
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R package are not installed in the default directories because of lack of admin rights. More precisely, lib_path is passed through the new argument of .libPaths() so that the new library paths are unique(c(new, .Library.site, .Library)). Warning: .libPaths() is restored to the initial paths, after function execution. Ignored if NULL (default) or if the safer_check argument is FALSE: only the pathways specified by the current .libPaths() are used for package calling.
- error_text
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: error_text = " INSIDE <PACKAGE_1>::<FUNCTION_1> INSIDE <PACKAGE_2>::<FUNCTION_2>.". If NULL, converted into "".
Value
The function returns the error, warning, or standart message, as a single character string, depending on what has been selected using the kind argument and if such message exists.
NULL if no selected message returned and the print_no argument is FALSE.
The following message if no selected message returned and the print_no argument is TRUE: "NO (ERROR|WARNING|STANDARD) MESSAGE REPORTED".
The following message if 1) the kind argument is "warning" or "message" while an error message is returned and 2) the print_no argument is TRUE: "NO POTENTIAL (WARNING|STANDARD) MESSAGE BECAUSE OF ERROR MESSAGE REPORTED".
Details
WARNINGS
Only the first standard/error/warning message is returned.
Always use the env argument when get_message() is used inside functions.
The function does not prevent printing, for instance if print() or show() is used inside the instruction tested. To prevent that, use tempo <- utils::capture.output(error <- get_message(data = "arg_check(data = 'a', class = mean, neg_values = FALSE, print = TRUE)")). The return of get_message() is assigned into error and the printed messages are captured by utils::capture.output() and assigned into tempo. See the examples.
See also
try
.
Examples
get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)", kind = "error", header = TRUE, print_no = FALSE, text = NULL, env = NULL, safer_check = TRUE, lib_path = NULL, error_text = "")
#> NULL
get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)", kind = "warning",
print_no = TRUE, text = "IN A")
#> [1] "WARNING MESSAGE REPORTED IN A:\nIn wilcox.test.default(c(1, 1, 3), c(1, 2, 4), paired = TRUE): cannot compute exact p-value with zeroes\n"
get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)", kind = "message",
print_no = TRUE, text = "IN A")
#> [1] "NO STANDARD MESSAGE REPORTED IN A"
get_message(data = "wilcox.test()", kind = "message", print_no = TRUE, text = "IN A")
#> [1] "NO POTENTIAL STANDARD MESSAGE BECAUSE OF ERROR MESSAGE REPORTED IN A"
get_message(data = "wilcox.test()", kind = "error", print_no = TRUE, text = "IN A")
#> [1] "ERROR MESSAGE REPORTED IN A:\nIn wilcox.test.default() : argument \"x\" is missing, with no default\n"
get_message(data = "sum(1)", kind = "error", print_no = TRUE, text = "IN A")
#> [1] "NO ERROR MESSAGE REPORTED IN A"
get_message(data = "message('ahah')", kind = "error", print_no = TRUE, text = "IN A")
#> [1] "NO ERROR MESSAGE REPORTED IN A"
get_message(data = "message('ahah')", kind = "message", print_no = TRUE, text = "IN A")
#> ahah
#> [1] "NO STANDARD MESSAGE REPORTED IN A"
get_message(data = "ggplot2::ggplot(data = data.frame(X = 1:10, stringsAsFactors = TRUE),
mapping = ggplot2::aes(x = X)) + ggplot2::geom_histogram()", kind = "message", print_no = TRUE,
text = "IN INSTRUCTION 1")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> [1] "NO STANDARD MESSAGE REPORTED IN INSTRUCTION 1"