This function checks whether a file exists and, depending on the mode, performs additional checks. If the mode is set to "write", it checks whether the file exists. If it exists and force is FALSE, an error is raised. If force is TRUE, the existing file is overwritten or created if it doesn't exist. If the mode is set to "read", it checks whether the file exists. If it doesn't, an error is raised.

check_this_file(file_path, mode = c("write", "read"), force = FALSE)

Arguments

file_path

The path to the file to be checked. If NULL, an error is raised.

mode

The mode of operation. Choose from "write" or "read".

force

Logical indicating whether to force the operation (default is FALSE).

Examples

if (FALSE) {
# Example with a temporary file for writing
temp_file_write <- tempfile(fileext = ".txt")
check_this_file(temp_file_write, mode = "write")  # No error if file exists, force = TRUE

# Example creating a file with file.create() and then checking it for writing
temp_file_create <- tempfile(fileext = ".txt")
file.create(temp_file_create)
check_this_file(temp_file_create, mode = "write", force = TRUE)  # No error if file exists, force = TRUE

# Example checking a non-existing file for reading
temp_file_read <- tempfile(fileext = ".txt")
check_this_file(temp_file_read, mode = "read")  # Raises an error if file does not exist
}