You are having problems with the visibility engine caused by primitive functions. Consider:
> length.x <- function(x) invisible(23) > length(structure(1:10, class="x")) [1] 23 > mean.x <- function(x) invisible(23) > mean(structure(1:10, class="x")) > # no output
length is primitive, but mean not. From R Internals :
Is the return value of the top-level expression R controlled by the global logical variable R_Visible. This value is set to true (true) or false (false) when entering all primitive and internal functions based on the eval column of the table in the src / main / names.c file: the corresponding parameter can be extracted with the PRIMPRINT macro.
and
Internal and primitive functions force the documented configuration of R_Visible upon return, unless the C code can change it (the exceptions noted above are indicated by PRIMPRINT with a value of 2).
Thus, it seems that you cannot force invisible returns from primitive generics like [ , length , etc., and you should resort to workarounds like the ones Alex suggested.
source share