%||%运算符在R中的含义是什么?

时间:2021-01-11 22:27:48

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!

谢谢你的帮助!

1 个解决方案

#1


2  

%||% is not part of the R language. A quick search on GitHub for the code provided leads to the rlang package.

%||%不是R语言的一部分。在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。

#1


2  

%||% is not part of the R language. A quick search on GitHub for the code provided leads to the rlang package.

%||%不是R语言的一部分。在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。