我正在阅读 Hadley 的 Advanced R。在第 8 章中,他说我们可以使用以下方法从环境中删除对象:rm()
。但是,移除该物体后我仍然可以看到该物体。
这是我的代码:
e<- new.env()
e$a<-1
e$b<-2
e$.a<-3
e$c<-4
ls(e,all.names = TRUE)
#remove the object c
rm("c",envir = e)
ls(e,all.names = TRUE) #Doesn't exist here
#does the variable exist?
exists("c",envir = e) #Shows TRUE. Why is this?
exists("m",envir = e) #FALSE
ls(e,all.names = TRUE)
ls(e)
正如我们在上面看到的,理想情况下,我期望exists("c", envir = e)
回来FALSE
.
有什么想法吗?提前致谢。
From help(exists)
:
If inherits
is TRUE
并且没有找到值x
在指定的环境中,搜索环境的封闭框架,直到名称x
遇到。
命名变量时要小心。您与基本功能有冲突c()
。自从inherits = TRUE
是默认值,搜索封闭环境,在本例中是基本函数c()
被发现,这会产生TRUE
结果。因此,仅搜索环境e
然后退出,使用inherits = FALSE
.
exists("c", envir = e, inherits = FALSE)
# [1] FALSE
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)