Skip to content
Related Articles

Related Articles

Improve Article

How to Delete a File using R

  • Last Updated : 30 Jun, 2021
Geek Week

In this article, we will discuss how to delete a file in R Programming Language.

Directory in use:

Method 1: Using file.remove()

This is the simplest approach to delete a file as in this approach the user just needs to call the file.remove() function which is a bases function of the R programming language and in this function, the user just has to pass the path/name of the file which is to be deleted as the parameter of this function and by this, the user is able to delete the file as per the path/name provided.

file.remove() function is used to delete the file.



Syntax:

file.remove(path)

Parameter:

  • path: A character vector of one or more paths.

Example 1:

R




file.remove('gfg_data1.csv')

Output:

[1] TRUE

Example 2:



R




file.remove('gfg_data2.csv')

Output:

[1] TRUE

Method 2: Using unlink()

In this approach to delete a file, the user just needs to call unlink() function which is a bases function of the R programming language with the path/name of the file which is to be deleted as the parameter of this function and by this, the user is able to delete the file as per the path/name provided.

Unlink() function deletes the file(s) or directories specified by x.

Syntax:

unlink(x, recursive = FALSE, force = FALSE)

Parameters:

  • x: a character vector with the names of the file(s) or directories to be deleted.
  • recursive: logical. Should directories be deleted recursively?
  • force: logical. Should permissions be changed (if possible) to allow the file or directory to be removed?

Example:

R




unlink('gfg_data3.csv', recursive = FALSE, force = FALSE)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :