How to Delete a File with R

what is the fastest way to delete files using R

unlink() accepts wildcards, so you can do the following, which seems quite fast on my system:

system.time({ unlink('*.rds'); }); ## deleted 4000 ~65KB files
## user system elapsed
## 0.140 0.922 1.151

Note that @Thomas's suggestion of using system() with wait=F is a good idea, but has several drawbacks: (1) it is platform-dependent, (2) you will not be able to check the return code of the removal command, since it is run asynchronously, and (3) it may introduce a race condition; for example, if subsequent code quickly writes a new *.rds file, then it could end up being deleted by the asynchronous removal command.

Deleting files in folder and its sub folders in R

fold <- 'C:/some/path/here'

# get all files in the directories, recursively
f <- list.files(fold, include.dirs = F, full.names = T, recursive = T)
# remove the files
file.remove(f)

Delete files with certain name in all subdirectories

You can use file.remove directly on vector of files.

Using the pattern 'rainy weather.*\\.txt' you can select all the files that have 'rainy weather' in their name and end with '.txt'. recursive = TRUE is needed to make it work in subdirectories and full.names = TRUE will give the complete path of the filenames to be deleted.

file.remove(list.files(pathway, pattern = 'rainy weather.*\\.txt$', 
recursive = TRUE, full.names = TRUE))

Automatically Delete Files/Folders

Maybe you're just looking for a combination of file.remove and list.files? Maybe something like:

do.call(file.remove, list(list.files("C:/Temp", full.names = TRUE)))

And I guess you can filter the list of files down to those whose names match a certain pattern using grep or grepl, no?

Recoverable file deletion in R

I wouldn't know about a solution that is fully compatible with Windows' "recycle bin", but if you're looking for something that doesn't quite delete files, but prevents them from being stored indefinitely, a possible solution would be to move files to the temporary folder for the current session.

The command tempdir() will give the location of the temporary folder, and you can just move files there - to move files, use file.rename().

They will remain available for as long as the current session is running, and will automatically be deleted afterwards . This is less persistent than the classic recycle bin, but if that's what you're looking for, you probably just want to move files to a different folder and delete it completely when you're done.

For a slightly more consistent syntax, you can use the fs package (https://github.com/r-lib/fs), and its fs::path_temp() and fs::file_move().

How can I create a condition to delete the files?

You can use purrr::keep

result <- purrr::keep(images, ~image_variance(.x) > 800)

Or base R Filter to keep only those images when their variance is greater than 800.

result <- Filter(function(x) image_variance(x) > 800, images)


Related Topics



Leave a reply



Submit