From 886c71ec07492c41347c26e449a812c6c5a85025 Mon Sep 17 00:00:00 2001 From: flavis Date: Thu, 3 Jun 2021 16:15:13 +0200 Subject: [PATCH] add live 6 --- sose2021/r/w06/L06-1.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sose2021/r/w06/L06-1.R diff --git a/sose2021/r/w06/L06-1.R b/sose2021/r/w06/L06-1.R new file mode 100644 index 0000000..b4bc20c --- /dev/null +++ b/sose2021/r/w06/L06-1.R @@ -0,0 +1,27 @@ +clear_warnings <- function() last.warning <<- list() + +careless <- function(f) { + function(...) { + suppressWarnings(f(...)) + } +} + +fail_with <- function(expr, value) tryCatch(expr, error = function(cond) value) +does_error <- function(expr) { + didFail <- F + tryCatch(expr, error = function(cond) didFail <<- T) + didFail +} + +count_warnings <- function(expr) { + N <- 0 + withCallingHandlers(expr, warning = function(cond) N <<- N + 1) + N +} + +record <- function(expr) { + msgs <- c() + rec <- function(cond) { msgs <<- c(msgs, cond$message); rlang::cnd_muffle(cond)} + withCallingHandlers(expr, warning = rec, message = rec) + msgs +}