Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, what is the difference between unlink and file.remove?

Tags:

r

R provides two functions to remove files (and folder) from the file system:

  • unlink
  • file.remove

It's not entirely obvious what the differences are, or indeed when to use which, other than that unlink takes some additional arguments.

Reading the source code for these functions doesn't help very much, since both simply calls a compiled C function.

What are the differences? When should you use unlink in preference to file.remove, or vice versa?

like image 541
Andrie Avatar asked Mar 27 '12 11:03

Andrie


People also ask

Does unlink remove a file?

The unlink function deletes the file name filename . If this is a file's sole name, the file itself is also deleted. (Actually, if any process has the file open when this happens, deletion is postponed until all processes have closed the file.)

What is unlink in R?

Delete File in R The unlink() function takes a maximum of three parameters and removes the specified file or directory. The unlink(x, recursive = TRUE) function deletes the just symbolic link if the target of such a link is a directory.

What does it mean to unlink a file?

unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.

What is the use of unlink command?

The unlink command performs the unlink subroutine on a specified file. The unlink command does not issue error messages when the associated subroutine is unsuccessful; you must check the exit value to determine if the command completed normally.


2 Answers

My guess is simply that unlink was present in S, since it references Becker, Chambers and Wilks (1988), whereas file.remove (and file.copy, file.create, etc.) have been part of R since early on in order to provide a family of functions for general file manipulation.

unlink corresponds to a very old Unix function (and certainly existed when the 1988 version of S appeared): http://en.wikipedia.org/wiki/Unlink_(Unix)

So, unlink is there for compatibility with S, file.remove is there as a part of R and both are maintained in order to support long existing code from S and R. Other than that simply choose the one that suits your needs (or habits) best.

like image 161
mdsumner Avatar answered Oct 06 '22 01:10

mdsumner


I believe that the main practical difference in the behaviour of the functions is how they handle a file that is currently open, and thus which cannot be immediately deleted:

  • unlink() marks the file for deletion as soon as it is not open in any other context;
  • file.remove() leaves the file intact, returning FALSE, but throws a warning.

Related: how to remove the file when "permisson denied" in R?

like image 25
Martin Smith Avatar answered Oct 05 '22 23:10

Martin Smith