R if file exists and is not a directory

I have an R script that accepts a file as input, and I need a general way to find out if the input is a file that exists and is not a directory.

In Python, you will do this: How to check if a file exists using Python? but I tried to find something similar in R.

What I would like is something like below, assuming file.txtthere is:

input.good = "~/directory/file.txt"
input.bad = "~/directory/"

is.file(input.good) # should return TRUE
is.file(input.bad) #should return FALSE

R has something called file.exists (), but that does not distinguish files from directories.

+9
source share
3 answers

The solution is to use file_test ()

.

.

input.good = "~/directory/file.txt"
input.bad = "~/directory/"

file_test("-f", input.good) # returns TRUE
file_test("-f", input.bad) #returns FALSE

:

file_test (op, x, y)

op , . ( x): "-f" ( ), "-d" ( ) "-x" ( ). "-nt" ( , ) "-ot" ( ): , .

x, y , .

+10

R dir.exists.

file.exists(f) && !dir.exists(f)
+9

is_file(path) fs.

+2
source

Source: https://habr.com/ru/post/1685411/


All Articles