Does R have a way to check if a file exists? And can you provide some examples of using this function? 2

Does R have a way to check if a file exists? And can you provide some examples of using this function?

R has a built-in function called “file.exists()” that allows users to check if a file exists in a particular directory. This function returns a boolean value of TRUE if the file exists and FALSE if it does not. It can be used for various applications such as checking if a data file is present before attempting to load it or verifying if a specific output file has been created. Examples of using this function include:
1. Checking if a file named “data.csv” exists in the current working directory: file.exists(“data.csv”)
2. Verifying if a file named “results.txt” exists in a specific folder: file.exists(“C:/Documents/Results/results.txt”)
3. Using a conditional statement to perform a certain action only if the file exists: if(file.exists(“my_file.pdf”)) {print(“File found!”)} else {print(“File does not exist.”)}

Check if File Exists in R (With Examples)


You can use the following basic syntax to check if a file exists in your current working directory in R:

file.exists('my_data.csv')

This function will return TRUE if the file exists or FALSE if it does not.

You can also use an if else statement to read a file into R only if it exists:

data <- 'my_data.csv'

if(file.exists(data)){  df <- read.csv(data)} else {  print('Does not exist')}

The following example shows how to use these functions in practice.

Example: Check if File Exists in R

Suppose my current in R is a folder called test_data with three CSV files:

I can use list.files() to list out the names of every file in the working directory:

#display the names of every file in current working directory
list.files()
[1] "my_data.csv"       "my_new_data.csv"   "some_old_data.csv"

I can use file.exists() to check if a given file exists in the current working directory:

#check if file 'my_data.csv' exists in current working directory
file.exists('my_data.csv')

[1] TRUE

The function returns TRUE, which tells us that the file ‘my_data.csv’ does indeed exist in the current working directory.

We can then use the following if else statement to import a file only if it exists:

#define file name
data <- 'my_data.csv'

#import file only if it exists
if(file.exists(data)){  df <- read.csv(data)} else {  print('Does not exist')}

#view contents of CSV file
df

  team points assists
1    A     14       4
2    B     26       7
3    C     29       8
4    D     20       3

Since the file exists, we’re able to import it successfully.

#define file name
data <- 'this_data.csv'

#import file only if it exists
if(file.exists(data)){  df <- read.csv(data)} else {  print('Does not exist')}

[1] "Does not exist"

We receive the message Does not exist”, which tells us that a file called this_data.csv does not exist in the current working directory.

Cite this article

stats writer (2024). Does R have a way to check if a file exists? And can you provide some examples of using this function?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/does-r-have-a-way-to-check-if-a-file-exists-and-can-you-provide-some-examples-of-using-this-function/

stats writer. "Does R have a way to check if a file exists? And can you provide some examples of using this function?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/does-r-have-a-way-to-check-if-a-file-exists-and-can-you-provide-some-examples-of-using-this-function/.

stats writer. "Does R have a way to check if a file exists? And can you provide some examples of using this function?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/does-r-have-a-way-to-check-if-a-file-exists-and-can-you-provide-some-examples-of-using-this-function/.

stats writer (2024) 'Does R have a way to check if a file exists? And can you provide some examples of using this function?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/does-r-have-a-way-to-check-if-a-file-exists-and-can-you-provide-some-examples-of-using-this-function/.

[1] stats writer, "Does R have a way to check if a file exists? And can you provide some examples of using this function?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. Does R have a way to check if a file exists? And can you provide some examples of using this function?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top