?invisible
says
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(...)))
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.
By trial and error:
# 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With