R帮助解释invisible()
作为“返回对象的暂时不可见副本的函数”。我很难理解什么invisible()
是用来。你能解释一下什么吗invisible()
这个功能何时有用?
我见过那个invisible()
几乎总是在方法函数中使用print()
。这是一个例子:
### My Method function:
print.myPrint <- function(x, ...){
print(unlist(x[1:2]))
invisible(x)
}
x = list(v1 = c(1:5), v2 = c(-1:-5) )
class(x) = "myPrint"
print(x)
我当时想如果没有invisible(x)
,我将无法完成如下任务:
a = print(x)
但实际情况并非如此。
所以我想知道什么invisible()
确实如此,它在哪里有用,最后它在上面的方法 print 函数中的作用是什么?
非常感谢您的帮助。
From ?invisible
:
Details:
This function can be useful when it is desired to have functions
return values which can be assigned, but which do not print when
they are not assigned.
所以你可以分配结果,但如果不分配则不会打印。它经常用来代替return
. Your print.myPrint
方法仅打印,因为您显式调用print
。致电给invisible(x)
在函数末尾仅返回一个副本x
.
如果你没有使用过invisible
, x
如果没有分配也会被打印。例如:
R> print.myPrint <- function(x, ...){
+ print(unlist(x[1:2]))
+ return(x)
+ }
R> print(x)
v11 v12 v13 v14 v15 v21 v22 v23 v24 v25
1 2 3 4 5 -1 -2 -3 -4 -5
v11 v12 v13 v14 v15 v21 v22 v23 v24 v25
1 2 3 4 5 -1 -2 -3 -4 -5
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)