invisible()返回的对象什么时候不可见?

zwo*_*wol 6 r language-lawyer

?invisible

返回对象的(临时)不可见副本.

这个括号内意味着隐形不会永远存在,但我找不到任何可以解释它何时消失的东西.我特别想知道像这样的结构(来自我的这个旧答案):

printf <- function(...) invisible(print(sprintf(...)))
Run Code Online (Sandbox Code Playgroud)

其中外部invisible可能是不必要的(因为print已经标明其返回值不可见的). withVisible()报告说这个函数的返回值在任何一种情况下都是不可见的,但我不知道这是否由语言保证,或者只是它在当前实现中的工作方式.

Kar*_*ner 2

通过反复试验:

# invisible
withVisible(invisible())$visible
[1] FALSE

### passing the invisible value through a function seems to
# preserve the invisibility
withVisible(identity(invisible()))$visible
[1] FALSE

# the <- operator just returns its arguments, so it confirms the above
withVisible(i <- invisible())$visible
[1] FALSE
# but the assigned value is no longer invisible
withVisible(i)$visible
[1] TRUE

### passing an invisible value as argument keeps the invisibility
f <- function(x) withVisible(x)$visible
f(1)
[1] TRUE
f(invisible(1))
[1] FALSE

### every other operation seems to cancel the invisibility.
# e.g. assigning an invisible value cancels the it
i <- invisible()
withVisible(i)$visible
[1] TRUE

withVisible(invisible(1) + 1)$visible
[1] TRUE
Run Code Online (Sandbox Code Playgroud)