Create a file path, creating the indicated directory if it does not yet exist.
Usage
create_file_path(
filename,
format_stamp = "%Y_%m_%d_%H_%M_%S",
dir = fs::path(".", "output"),
add_date = TRUE
)Arguments
- filename
character string with the file name, including the file extension like
.csvor.txt. Should adhere to the restrictions described incheckinput::is_path().- format_stamp
character string indicating the format of the stamp to be added in front of the file name. No stamp is added if
format_stampis an empty string (i.e.,""). The formatted stamp is treated as part of the filename, such that the same restrictions apply, seeDetails.- dir
Character string containing a valid path to a directory that should be created if it does not yet exist.
fs::path()adds file separators and the dot (".") indicates the working directory, such that by default a subdirectory with the current date in the formatYYYY_mm_ddin directoryoutputbelow the working directory is created.- add_date
TRUEorFALSE: create a subdirectory indirwith the current date in the formatYYYY_mm_dd?
Value
The created absolute normalized file path, returned invisibly.
Details
filename should contain a file extension (i.e., a dot followed by any
character until the end of the file name) and should not contain slashes
or backslashes: use dir to indicate subdirectories.
The absolute normalised path is returned such that the returned path still works if the working directory changes.
A warning is issued if the file indicated by the returned path already
exists. To prevent this when creating files in quick succession, use "%OSn"
as part of format_stamp to create precise stamps by truncating seconds to
0 <= n <= 6 decimal places, see strftime() for details.
The file is not created by create_file_path(), use
fs::file_create(create_file_path(filename = "abc.txt", ...)) or
file.create(create_file_path(filename = "abc.txt", ...)) to do so.
Side effects
The directory indicated by the returned path is created if it does not yet exist.
See also
checkinput::is_path() to check if a path is valid, with a Note on paths
and extensive references about file paths and directories;
get_file_path() to check if a file exists and is a unique match to a pattern;
create_dir() (used by this function) to create a directory if it does not
yet exist
Other functions to handle paths and directories:
create_dir(),
create_tempdir(),
get_file_path()
Examples
# Use a temporary directory to not write in the user's directory
my_tempdir <- create_tempdir(prefix = "examplecreatefilepath")
(create_file_path(filename = "abc.txt", format_stamp = "",
dir = my_tempdir, add_date = TRUE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/2026_07_02/abc.txt
(create_file_path(filename = "abc.txt", format_stamp = "%d_%m_%Y",
dir = my_tempdir, add_date = TRUE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/2026_07_02/02_07_2026_abc.txt
(create_file_path(filename = "def.html", format_stamp = "",
dir = my_tempdir, add_date = FALSE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/def.html
(create_file_path(filename = "def.html", format_stamp = "%d_%m_%Y",
dir = my_tempdir, add_date = FALSE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/02_07_2026_def.html
(create_file_path(filename = "abc.txt", format_stamp = "",
dir = fs::path(my_tempdir, "subdir"), add_date = TRUE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/subdir/2026_07_02/abc.txt
(create_file_path(filename = "abc.txt", format_stamp = "%d_%m_%Y",
dir = fs::path(my_tempdir, "subdir"), add_date = TRUE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/subdir/2026_07_02/02_07_2026_abc.txt
(create_file_path(filename = "def.html", format_stamp = "",
dir = fs::path(my_tempdir, "subdir"), add_date = FALSE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/subdir/def.html
(create_file_path(filename = "def.html", format_stamp = "%d_%m_%Y",
dir = fs::path(my_tempdir, "subdir"), add_date = FALSE))
#> /tmp/Rtmp31i27G/examplecreatefilepath1a6249978d00/subdir/02_07_2026_def.html
# Cleaning up
unlink(x = my_tempdir, recursive = TRUE)
rm(my_tempdir)