config_file
returns the absolute, normalized path to the configuration file of a program/package/app based on an optional app-specific commandline argument, an optional app-specific environment variable and the XDG Base Directory Specification
Usage
config_file(
app_name,
file_name,
cl_arg = commandArgs()[grep("--config-file", commandArgs()) + 1],
env_var = "",
sep = "/",
copy_dir = norm_path(xdg_config_home(), app_name),
fallback_path = NULL
)
Arguments
- app_name
Name of the program/package/app
- file_name
Name of the configuration file
- cl_arg
Value of app specific commandline parameter
- env_var
Value of app specific environment variable
- sep
Path separator to be used on Windows
- copy_dir
Path to directory where
$fallback_path
should be copied to in case it gets used.- fallback_path
Value to return as fallback (see details)
Details
The following algorithm is used to determine the location of $file_name
:
If
$cl_arg
is a non-empty string, return itElse, if
$env_var
is a non-empty string, return itElse, if
$PWD/.config/$app_name
exists, return itElse, if
$XDG_CONFIG_HOME/$app_name/$file_name
exists, return itElse, if
$HOME/.config/$app_name/$file_name
exists, return itElse, if
$USERPROFILE/.config/$app_name/$file_name
exists, return itElse, if
$copy_dir
is non-empty string and$fallback_path
is a path to an existing file, then try to copy$fallback_path
tocopy_dir
/$file_name
and returncopy_dir
/$file_name
(Note, that in case $copy_dir is a non-valid path, the function will throw an error.)Else, return $fallback_path
Examples
config_dir("myApp")
#> [1] "/home/runner/.config/myApp"