如何删除 R 中的 ffdf 对象目录?

How to delete ffdf objects directories in R?

我正在使用 ffdf 包进行一些数据预处理。我的工作计算机有 4 CPU 个内核和 8 Gb 内存,我可以处理大约 0.2-3 亿个数据点,这真是太棒了。

但是,我还有一个限制。大 ffdf 对象用完了我计算机的磁盘 space。当我将 5 个表合并为一个表并在其上添加 20 个列时,我收到一条消息 "no disk space"

所以,我想删除磁盘驱动器上未使用的 ffdf 对象。我可以在不退出 R 会话的情况下完成吗? (我对该文件夹具有读取、写入和执行权限。)

我已经尝试了我所知道的一切,例如 unlink()file.remove()delete()close()finalize()finalizer()ffdrop()".

delete_dir <- "d:/ff/t1_pre"
deletephrase <- "t1_pre"
id <- grep(deletephrase, dir(delete_dir))
todelete <- dir(delete_dir, full.names=T)[id]
todelete
# [1] "d:/ff/t1_pre/t1_pre$b_cnt.ff"     "d:/ff/t1_pre/t1_pre$b_rct.ff"     "d:/ff/t1_pre/t1_pre$b_weit.ff"   
# [4] "d:/ff/t1_pre/t1_pre$deal_id.ff"   "d:/ff/t1_pre/t1_pre$m_id.ff"      "d:/ff/t1_pre/t1_pre$saled_qty.ff"

unlink(todelete)  #only delete .rdata and .rprofile

file.remove(todelete)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE
# Warning messages:
# 1: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$b_cnt.ff', reason 'Permission denied'
# 2: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$b_rct.ff', reason 'Permission denied'
# 3: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$b_weit.ff', reason 'Permission denied'
# 4: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$deal_id.ff', reason 'Permission denied'
# 5: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$m_id.ff', reason 'Permission denied'
# 6: In file.remove(todelete) :
#   cannot remove file 'd:/ff/t1_pre/t1_pre$saled_qty.ff', reason 'Permission denied'

ffdrop("d:/ff/t2_pre/t2_pre$c_cnt.ff")
# $RData
# d:/ff/t2_pre/t2_pre$c_cnt.ff.RData 
#                              FALSE 

# $ffData
# d:/ff/t2_pre/t2_pre$c_cnt.ff.ffData 
#                               FALSE 

# Warning messages:
# 1: In file.remove(imgfile) :
#   cannot remove file 'd:/ff/t2_pre/t2_pre$c_cnt.ff.RData', reason 'No such file or directory'
# 2: In file.remove(zipfile) :
#   cannot remove file 'd:/ff/t2_pre/t2_pre$c_cnt.ff.ffData', reason 'No such file or directory'

finalize(t2_pre)
# $m_id
# [1] FALSE

# $deal_id
# [1] FALSE

close(t2_pre)
# [1] FALSE

finalizer(t2_pre)
# Error in UseMethod("finalizer") : 
#   no applicable method for 'finalizer' applied to an object of class "ffdf"

delete(t2_pre)
# [1] FALSE
# Warning messages:
# 1: In file.remove(attr(physical, "filename")) :
#   cannot remove file 'd:/ff/t2_pre/t2_pre$m_id.ff', reason 'Permission denied'

您首先需要关闭您的 ff 文件,然后才能执行 file.remove。 close(yourffdf)close(yourffobject) 和下一个 file.remove(list.files(yourpath), recursive=TRUE, full.names=TRUE) 之类的

require(ff)
x <- as.ffdf(iris)
sapply(filename(x), file.remove) ## Will fail
close(x)
sapply(filename(x), file.remove) ## Works