6

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

Community
  • 1
  • 1
zwol
  • 135,547
  • 38
  • 252
  • 361
  • it looks the C code simply returns its argument. So, by temporary, it may just mean anything returned from within the `invisible` call won't print, but something like `printf <- function(...) +invisible(1); printf(1)` will still print `1`, whereas `printf <- function(...) invisible(1); printf(1)` wont – Rorschach Sep 14 '15 at 21:51

1 Answers1

2

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
Karl Forner
  • 4,175
  • 25
  • 32
  • This is useful information, but I am really hoping for an answer from the *language specification* (to the extent R has one) rather than the observed behavior of the implementation. In other words, we can see what it *does*, but is that what it is *supposed to do*, and will it stay that way? – zwol Oct 14 '15 at 19:29