find_safe_path: Safe Save

Description Usage Arguments Details Value Examples

View source: R/safe_save.R

Description

Small tool consisting of a single function that helps find a file or folder name that does not already exist

Usage

1
find_safe_path(path, is_dir = FALSE, create_folder = FALSE, recursive = TRUE)

Arguments

path

string for path to check and, if necessary, alter

is_dir

boolean if FALSE specified path is for a file, if TRUE specified path is a folder path. Default is FALSE (i.e., file path).

create_folder

boolean that is optional and if TRUE, creates a folder where necessary, if FALSE, returns a warning if a folder needs to be created. Concerns the parent folder if the path concerns a file (i.e., is_dir = TRUE) and the directory path based on the specified directory path otherwise. Default is FALSE.

recursive

boolean if TRUE parent folders are also created if they do not yet exists, if FALSE only the last folder in the path is created. Default is TRUE. Parameter is silently ignored if the parameter create_folder is set to FALSE. Default is TRUE.

Details

The find_safe_path function checks whether the specified path exists. If the file or folder does not exist, the specified path is returned to the user. If the folder does not exist and create_folder is set to TRUE, the folder (for a path to a folder) or parent folder (for a path to a file) is created. If the specified path does exist a numeric suffix will be added that does not already exist. The search for a suffix starts with 1 and increases incrementally. It is placed in brackets and, if applicable, comes before the file extension. Thus, the file path "/path/to/file.docx" could become "/path/to/file (1).docx", if the latter does not already exist.

Value

non-existent path equal to or based on the specified file path

Examples

1
2
dir_path <- find_safe_path(file.path("path", "to"), is_dir=TRUE, create=TRUE)  # Create a folder, first ensuring that it does not already exist
file_path <- find_safe_path(file.path("path", "to", "file", "data.rds"))  # Check whether there is a file "data.rds" in the specified folder and get a non-existent file name if it does

cisglee/safeSave documentation built on Dec. 19, 2021, 4:59 p.m.