R How to Test Whether File Exists in Local Directory (2 Examples)

This article shows how to test whether a file exists in a local directory in R programming.

Example 1: Testing for Existence of Local File

file.exists("my_df.csv")             # Using file.exists function
my_df <- data.frame(vec1 = 10:20,    # Creating some example data
                    vec2 = letters[10:20])
write.csv2(my_df, "my_df.csv")       # Writing example data to local file
file.exists("my_df.csv")             # Using file.exists function again

Example 2: Testing Whether Local File Does Not Exist

!file.exists("my_df.csv")            # file.exists function with ! in front

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed