如何在R中删除目录? [英] How to remove a directory in R?
问题描述
经过一番研究,我发现以下工作原理:
After some research I found out that the following works:
unlink("mydir")
,如果要删除,必须使用 recursive
选项递归:
and you have to use the recursive
option in case you want to remove recursively:
unlink("mydir", recursive=TRUE)
但是,我注意到仅 unlink( mydir)
,没有递归
选项在 mydir
包含子目录时不产生任何输出:它不删除目录,但不显示任何警告。只是什么都没有:
However, I noted that unlink("mydir")
alone, without the recursive
option, does not produce any output when mydir
contains subdirectories: it does not remove the dirs but does not show any warning. Just nothing:
> list.dirs()
[1] "." "./r"
> dir.create("test")
> dir.create("test/test2")
> list.dirs()
[1] "." "./r" "./test" "./test/test2"
> unlink("test") ######### here I would expect a warning #########
> list.dirs()
[1] "." "./r" "./test" "./test/test2"
> unlink("test", recursive=TRUE)
> list.dirs()
[1] "." "./r"
有没有办法获得任何类型的通知,例如您会进入UNIX系统吗?
Is there any way to get any kind of "notification", like the one you would get in UNIX systems?
$ rmdir test
rmdir: failed to remove «test»: Directory not empty
我正在使用R版本3.1.2(2014-10-31)。我试着玩 options(warn = 1)
等,但是没有运气。
I am using R version 3.1.2 (2014-10-31). I tried playing with options(warn=1)
etc but no luck.
推荐答案
查看帮助?取消链接
:
值
Value
0表示成功,1表示失败,这是无形的。如果
递归= FALSE,则不删除不存在的
文件也不是失败,也不能删除目录。但是,x中缺少的值将被视为
失败。
0 for success, 1 for failure, invisibly. Not deleting a non-existent file is not a failure, nor is being unable to delete a directory if recursive = FALSE. However, missing values in x are regarded as failures.
如果存在文件夹 foo
没有 recursive = TRUE
的 unlink
调用将返回 1
。
In the case where there is a folder foo
the unlink
call without recursive=TRUE
will return 1
.
请注意,实际上,该行为更像是 rm -f
,这意味着取消链接不存在的文件将返回0。
Note that actually the behavior is more like rm -f
, which means that unlinking a non-existent file will return 0.
这篇关于如何在R中删除目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!