在下面的 R 代码片段(取自调试器(中,%||% 运算符的意思是,在第 8 行?
function (env = caller_env(), default = NULL)
{
out <- switch_type(env, environment = env, definition = ,
formula = attr(env, ".Environment"), primitive = base_env(),
closure = environment(env), list = switch_class(env,
frame = env$env))
out <- out %||% default
if (is_null(out)) {
type <- friendly_type(type_of(env))
abort(paste0("Can't extract an environment from ", type))
}
else {
out
}
}
感谢您的帮助!
%||%
不是R语言的一部分。在 GitHub 上快速搜索提供的代码会导致rlang
包。
library(rlang)
`%||%`
导致:
function (x, y)
{
if (is_null(x))
y
else x
}
<environment: namespace:rlang>
换句话说,如果不NULL
,它会返回左侧,否则返回右侧。
这个运算符在整洁中被广泛使用。