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 # FALSE |
file.exists("my_df.csv") # Using file.exists function # FALSE
my_df <- data.frame(vec1 = 10:20, # Creating some example data vec2 = letters[10:20]) |
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 |
write.csv2(my_df, "my_df.csv") # Writing example data to local file
file.exists("my_df.csv") # Using file.exists function again # TRUE |
file.exists("my_df.csv") # Using file.exists function again # TRUE
Example 2: Testing Whether Local File Does Not Exist
!file.exists("my_df.csv") # file.exists function with ! in front # FALSE |
!file.exists("my_df.csv") # file.exists function with ! in front # FALSE