简体   繁体   English

%||%运算符在R中是什么意思?

[英]What does the %||% operator mean in R?

In the below R code snippet (taken from a debugger), what does the %||% operator mean, in line 8? 在下面的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
  }
}

Thanks for your help! 谢谢你的帮助!

%||% is not part of the R language. %||%不是R语言的一部分。 A quick search on GitHub for the code provided leads to the rlang package. 在GitHub上快速搜索提供的代码会生成rlang包。

library(rlang)
`%||%`

leads to: 导致:

function (x, y) 
{
    if (is_null(x)) 
        y
    else x
}
<environment: namespace:rlang>

in other words, it returns the left side if it's not NULL and the right side otherwise. 换句话说,如果不为NULL ,则返回左侧,否则返回右侧。

This operator is widely used in the tidyverse. 该运算符在tidyverse中被广泛使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM