3.测试:Adverbs take a function and return a modified function.
3.1 silently()
#@ silently() transforms a function so that when you call this new function, it returns nothing unless there is an error or a warning (contrary to attempt that returns the result). In a sense, the new function stay silent unless error or warning. silent_log <- silently(log) silent_log(1) silent_log(“a”)
Error in .f(…) : 数学函数中用了非数值参数
#@ With silently(), the result is never returned. silent_matrix <- silently(matrix) silent_matrix(1:3,2)
Warning message:
In .f(…) : 数据长度[3]不是矩阵行数[2]的整倍
3.2 surely()
#@ surely() transforms a function so that when you call this new function, it calls attempt() - i.e. in the code below, calling sure_log(1) is the same as calling attempt(log(1)). In a sense, you’re sure this new function will always work. sure_log <- surely(log) sure_log(1)
[1] 0
sure_log(“a”)
Error: 数学函数中用了非数值参数
3.3 with_message() and with_warning()
#@ These two functions take a function, and add a warning or a message to it. as_num_msg <- with_message(as.numeric, msg = “We’re performing a numeric conversion”) as_num_warn <- with_warning(as.numeric, msg = “We’re performing a numeric conversion”) as_num_msg(“1”)
We’re performing a numeric conversion
[1] 1
as_num_warn(“1”)
[1] 1
Warning message:
In as_num_warn(“1”) : We’re performing a numeric conversion
3.4 without_message(), without_warning(), and discretly()
#@ These three functions do the opposite, as they remove warnings and messages: matrix(1:3, ncol = 2)
attempt包测试3_Adverbs_20200204Tuesday1.设置当前工作目录getwd()setwd("./attempt/")2.导入R包library(attempt)3.测试:Adverbs take a function and return a modified function.3.1 silently()#@ silently() transforms ...