What is meaning of the syntax %||%?

吃可爱长大的小学妹 提交于 2020-05-24 06:06:31

问题


I'm trying to create a dynamic UI, so i used this code

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

from :

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

My question is basically, what is meaning of the syntax %||% ?


回答1:


The help page of sheds light into it:

?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.

It is similar to a coalesce function. Basically, whenever the input is NULL, that means it is not (yet) available, an empty tring is assigned rather than NULL. This is desireable because this is rendered and displayed as an empty value should. NULL would be shown like an error massage




回答2:


The %||% comes from rlang. You can find it on the ?"op-null-default" help page. From the documentation

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.

Basically it returns the second value if the first is NULL.



来源:https://stackoverflow.com/questions/60418258/what-is-meaning-of-the-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!