什么时候由invisible()返回的对象停止不可见? [英] When does the object returned by invisible() cease to be invisible?
问题描述
?invisible
说
返回(暂时)对象的不可见副本.
Return a (temporarily) invisible copy of an object.
该括号表示隐身不会永远持续下去,但是我找不到能解释消失的任何信息.我特别想知道这样的结构(来自这个旧的答案我的):
That parenthetical implies that the invisibility will not last forever, but I can't find anything that explains when it goes away. I'm particularly wondering about constructs like this one (from this old answer of mine):
printf <- function(...) invisible(print(sprintf(...)))
外面的invisible
可能 不必要(因为print
已经将其返回值标记为不可见). withVisible()
报告说该函数的返回值是不可见的,但是我不知道该语言是否能保证该返回值,或者不知道它在当前实现中的工作方式.
where the outer invisible
is probably unnecessary (because print
already marked its return value invisible). withVisible()
reports that this function's return value is invisible either way, but I don't know whether that is guaranteed by the language, or just the way it happens to work in the current implementation.
推荐答案
通过反复试验:
# 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
这篇关于什么时候由invisible()返回的对象停止不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!