Luc*_*aia 1 workspace garbage-collection r
我正在尝试创建一个函数,该函数将同时清除工作区和内存,这样我就不必键入“rm(list = ls()); gc()”,而只需键入一个函数。但是 rm(list = ls()) 从函数内部调用时不起作用。为什么?有没有办法解决?
> # Let's create an object
> x = 0
> ls()
[1] "x"
>
> # This works fine:
> rm(list = ls()); gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 269975 14.5 592000 31.7 427012 22.9
Vcells 474745 3.7 1023718 7.9 808322 6.2
> ls()
character(0)
>
> ## But if I try to create a function to do exactly the same thing, it doesn't work
> # Creating the object again
> x = 0
> ls()
[1] "x"
>
> #Here's the function (notice that I have to exclude the function name from the
# list argument or the function would remove itself):
> clear = function(list = ls()[-which(ls() == "clear")]){
+ rm(list = list); gc()
+ }
> ls()
[1] "clear" "x"
>
Run Code Online (Sandbox Code Playgroud)
小智 5
rm 实际上正在工作,但是由于您在函数内部使用它,因此它只会删除与该函数环境有关的所有对象。
envir = .GlobalEnv向两个调用添加参数:
rm(list = ls(envir = .GlobalEnv), envir = .GlobalEnv)
Run Code Online (Sandbox Code Playgroud)
应该这样做。
我还建议你看看这个约GC(),因为我认为这不是一个很好的做法,显式调用它,除非你的其他问题确实需要它。
| 归档时间: |
|
| 查看次数: |
1364 次 |
| 最近记录: |