This is a simple function that helps with the case where you want to construct a filename (usually for an image) with the same base of the filename, the same directory (default), but things added to the front or end of that base filename, with the same extension.

remap_filename(x, sub_dir = NULL, prefix = "", suffix = "")

Arguments

x

input filename/character vector

sub_dir

sub-directory for the new filename. If NULL, then the directory is the the same directory as x

prefix

string to put in front of base of filename

suffix

string to put at the end of base of filename

Value

Character vector

Examples

fname = file.path("/path/to/file", "original.nii.gz")
remap_filename(fname, prefix = "preproc_", "_with_gz")
#> [1] "/path/to/file/_with_gz/preproc_original.nii.gz"
fname = "original.nii"
remap_filename(fname, prefix = "note_", "_has_directory")
#> [1] "./_has_directory/note_original.nii"
remap_filename(c(fname, "other.nii.gz"), prefix = "note_", "_has_directory")
#> [1] "./_has_directory/note_original.nii" "./_has_directory/note_other.nii.gz"