Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the object returned by invisible() cease to be invisible?

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

like image 654
zwol Avatar asked Sep 14 '15 20:09

zwol


1 Answers

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
like image 148
Karl Forner Avatar answered Oct 01 '22 19:10

Karl Forner