R 程式設計/管理工作區
外觀
< R 程式設計
此頁面解釋如何管理您的工作空間。
ls()列出您的工作空間中的物件。list.files()列出工作空間資料夾中的檔案。rm()從您的工作空間中刪除物件;rm(list = ls())刪除所有物件。
rm(list=ls()) # remove all the objects in the workspace
可以使用 save() 函式將每個物件儲存到磁碟。然後可以使用 load() 將它們載入到記憶體中。
load("file.Rda")
...
# assume you want to save an object called 'df'
save(df, file = "file.Rda")
save.image()儲存您的工作空間。
sessionInfo()提供有關您會話的資訊,例如載入的包、R 版本等。R.version提供有關 R 版本的資訊。
注意:根據 Linux 和 Mac 上的 R 版本 3.5.1,memory.size() 和 memory.limit() 是 Windows 特定的。
memory.size() 提供 R 當前使用的總記憶體量。
> memory.size()
[1] 10.18
memory.limit() 不帶任何引數提供 R 使用的記憶體限制。這也可以用來增加限制。最大數量受計算機記憶體的限制。
> memory.limit()
[1] 1535
> memory.limit(size=2000) # 2000 stands for 2000 MB
[1] 2000
object.size() 返回 R 物件的大小。您可以列印結果並選擇單位(位元組、千位元組、兆位元組等)。
> a <- rnorm(10^7)
> object.size(a)
80000024 bytes
> print(object.size(a),units="b")
80000024 bytes
> print(object.size(a),units="Kb")
78125 Kb
> print(object.size(a),units="Mb")
76.3 Mb
> print(object.size(a),units="Gb")
0.1 Gb
> print(object.size(a),units="auto")
76.3 Mb
memory.profile() 返回更多詳細資訊。
> memory.profile()
NULL symbol pairlist closure environment promise
1 4959 61794 1684 255 3808
language special builtin char logical integer
14253 46 687 5577 2889 4060
double complex character ... any list
523 1 11503 0 0 1024
expression bytecode externalptr weakref raw S4
1 0 497 117 118 642
gc()啟動垃圾收集器,它會導致 R 釋放不再使用的物件的記憶體。
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1095165 58.5 1770749 94.6 1770749 94.6
Vcells 12060564 92.1 17769683 135.6 12062095 92.1
