| pathFormat | R Documentation |
Generates a function that constructs file paths according to a path format (a template with placeholders). This is particularly useful in modeling workflows where outputs need to be organized by model name, with consistent naming conventions across files.
pathFormat(
path.format = "%name",
dir,
model = NULL,
script = NULL,
path.funs = NULL,
structure
)
path.format |
A character string specifying a path format template, or a custom function. If a function is provided, it is returned without modification. Templates are specified using
Any other combination of |
dir |
Character string specifying the base directory for all outputs. This is
prepended to all generated paths. Default is |
model |
Default value for the |
script |
Default value for the |
path.funs |
Optional named list of functions used to preprocess
|
structure |
Deprecated. Use |
The returned function accepts the following arguments:
nameThe base file name (typically without extension)
modelA model identifier (character string or model object). If a
model object (list) is provided, the $mod element is extracted and
used as the model name. The file extension is automatically removed from
the model name (e.g., "model.lst" becomes "model").
scriptA script identifier (character string, typically a script file name). The file extension is automatically removed (e.g., "myscript.R" becomes "myscript").
subdirOptional subdirectory. Inserted between the directory
components of path.format and the file name component.
dirBase directory. Overrides the dir argument passed to
pathFormat.
quietIf TRUE, suppress messages about ignored
model/script arguments. Default FALSE.
The generated function handles missing model/script arguments
gracefully - if path.format does not contain %model (or
%script), the corresponding argument is optional and ignored (with a
message if supplied anyway). If path.format does contain %model
(or %script), the corresponding argument is required.
A function with signature function(name, model, script, subdir, dir, quiet = FALSE) that
generates file paths according to the specified path format. If path.format
is already a function, it is returned unchanged.
writer for using path formats in writing functions,
ggwrite, ftwrite, lwrite for functions
that accept fun.path arguments
# Create a path function (default format)
path_fun <- pathFormat(path.format = "%model/%name_%model")
# Use it to generate paths
path_fun(name = "gof1.png", model = "run001")
# Returns: "run001/gof1_run001.png"
path_fun(name = "gof1.png", model = "run001", subdir = "diagnostics")
# Returns: "run001/diagnostics/gof1_run001.png"
# With base directory
path_fun2 <- pathFormat(path.format = "%model/%model-%name", dir = "outputs")
path_fun2(name = "gof1.png", model = "run001")
# Returns: "outputs/run001/run001-gof1.png"
# Different format
path_fun3 <- pathFormat(path.format = "%model-%name")
path_fun3(name = "gof1.png", model = "run001")
# Returns: "run001-gof1.png"
# Fully custom format
path_fun3b <- pathFormat(path.format = "archive/%model/plots/%name")
path_fun3b(name = "gof1.png", model = "run001")
# Returns: "archive/run001/plots/gof1.png"
# Format based on script name
path_fun3c <- pathFormat(path.format = "%script/%name_%model")
path_fun3c(name = "gof1.png", model = "run001", script = "myscript")
# Returns: "myscript/gof1_run001.png"
# Supplying default model/script to pathFormat() itself
path_fun3d <- pathFormat(path.format = "%script/%name_%model",
model = "run001", script = "myscript")
path_fun3d(name = "gof1.png")
# Returns: "myscript/gof1_run001.png"
# Explicit arguments to the generated function override the defaults
path_fun3d(name = "gof1.png", model = "run002")
# Returns: "myscript/gof1_run002.png"
# Custom preprocessing of script (and/or model) via path.funs
path_fun3e <- pathFormat(path.format = "%script/%name_%model",
path.funs = list(script = function(x) substr(basename(x), 1, 4)))
path_fun3e(name = "gof1.png", model = "run001", script = "myscript.R")
# Returns: "mysc/gof1_run001.png"
# Custom function (passed through unchanged)
my_fun <- function(name, model, subdir) {
paste0("custom/", model, "/", name)
}
path_fun4 <- pathFormat(path.format = my_fun)
identical(path_fun4, my_fun) # TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.