首页 > 解决方案 > 语法 %||% 的含义是什么?

问题描述

我正在尝试创建一个动态 UI,所以我使用了这段代码

output$col <- renderUI({
    map(col_names(), ~ textInput(.x, NULL, value = isolate(input[[.x]])) %||% "")
  })

从 :

https://mastering-shiny.org/action-dynamic.html#multiple-controls

我的问题基本上是,语法 %||% 的含义是什么?

标签: ruser-interface

解决方案


帮助页面揭示了它:

?rlang::`%||%`

Description
This infix function makes it easy to replace NULLs with a default value.
It's inspired by the way that Ruby's or operation (||) works.

Usage
x %||% y

Arguments
x, y    
If x is NULL, will return y; otherwise returns x.

它类似于合并函数。基本上,每当输入为 NULL 时,这意味着它(尚)不可用,分配一个空字符串而不是NULL. 这是可取的,因为它应该被渲染和显示为空值。NULL 会像错误消息一样显示


推荐阅读