Skip to contents

Checks if the directory containing the specified path exists, and creates it recursively if it does not. Provides console feedback using the cli package.

Usage

sys_make_path(path, showWarnings = FALSE, mode = "0777")

Arguments

path

A character string specifying the full path (intended for a file or directory). The function will ensure the parent directory of this path exists.

showWarnings

Logical. Should low-level warnings from dir.create() (e.g., if the directory already exists but with different permissions) be shown? Defaults to FALSE. Note that informational messages from this function itself are controlled separately (they always show).

mode

The mode to create the directory with, passed to dir.create(). Defaults to "0777" (read/write/execute for all). See ?dir.create.

Value

The original input path, returned invisibly. This is useful for piping, e.g., write.csv(my_data, makePath("/path/to/file.csv")).

Details

For a file path like "/path/to/my/file.txt", this function ensures the directory "/path/to/my/" exists. For a directory path like "/path/to/my/newdir/" or "/path/to/my/newdir", it ensures "/path/to/my/" exists. It does not create the final component (file.txt or newdir) itself, only its parent structure. Console messages indicate actions taken.

Examples

if (FALSE) { # \dontrun{
# --- Example 1: Path to a potential file ---
temp_file_path = file.path(tempdir(), "makePath_demo", "subdir", "my_data.csv")
cat("Demonstrating makePath for:", temp_file_path, "\n")
makePath(temp_file_path) # Should create makePath_demo/subdir/
makePath(temp_file_path) # Should report directory already exists

# --- Example 2: Path to a directory ---
temp_dir_path = file.path(tempdir(), "makePath_demo", "another_dir")
cat("\nDemonstrating makePath for:", temp_dir_path, "\n")
makePath(temp_dir_path) # Should NOT create 'another_dir', but ensure 'makePath_demo' exists

# --- Example 3: Path in current directory ---
cat("\nDemonstrating makePath for:", "local_file.txt", "\n")
makePath("local_file.txt") # Should give warning about not creating "."

# --- Example 4: Root directory ---
cat("\nDemonstrating makePath for:", "/", "\n")
makePath("/") # Should give warning about not creating "/"

# --- Clean up example directories ---
unlink(file.path(tempdir(), "makePath_demo"), recursive = TRUE)
} # }