How to remove a directory in R?

前端 未结 4 1904
有刺的猬
有刺的猬 2021-02-06 20:15

After some research I found out that the following works:

unlink(\"mydir\")

and you have to use the recursive option in case you w

相关标签:
4条回答
  • 2021-02-06 20:49

    Simply

    unlink("mydir") # will delete directory called 'mydir'
    
    0 讨论(0)
  • 2021-02-06 20:50

    For those stumbling on this, I normally resort to using 'shell' command here to completely delete the folder.

    Using 'system' will print a 127 error if the folder is non-empty.

    The following is the simple nuclear option - deleting the folder in its entirety (no questions asked):

    Loc <- "C:/file has spaces/hence the form below/"
    shell( glue::glue("rmdir /s /q \"{Loc}\" ") )
    
    0 讨论(0)
  • 2021-02-06 21:08

    Here's a wrapper function for you if you really need to see an error msg:

    .unlink <- function(x, recursive = FALSE, force = FALSE) {
      if (unlink(x, recursive, force) == 0)
        return(invisible(TRUE))
      stop(sprintf("Failed to remove [%s]", x))
    }
    
    0 讨论(0)
  • 2021-02-06 21:13

    See help ?unlink:

    Value

    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.

    In the case where there is a folder foo the unlink call without recursive=TRUE will return 1.

    Note that actually the behavior is more like rm -f, which means that unlinking a non-existent file will return 0.

    0 讨论(0)
提交回复
热议问题