| Title: | Manage Application Settings via '.env' or '.ini' Files |
|---|---|
| Description: | Provides a simple way to manage application settings by loading configuration values from '.env' or '.ini' files. It supports default values, type casting, and environment variable overrides, enabling a clean separation of configuration from code. Ideal for managing credentials, API keys, and deployment-specific settings. |
| Authors: | Morgan Durand [aut, cre, cph] |
| Maintainer: | Morgan Durand <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-27 10:24:47 UTC |
| Source: | https://github.com/dataupsurge/configular |
Helper function to cast data from keyword of function
cast_as(variable, cast)cast_as(variable, cast)
variable |
String to be casted |
cast |
Casting function or keyword |
casted variable
Config file search
find_config_files(path)find_config_files(path)
path |
The directory from which the config file search is initialized. Look in current and root directories until finding a config file reaching the primary root. |
the full path of the config file found. If no config file are found, it returns a 'NULL'.
configulaR currently supports only .env files. 'get_config' will search if the speficied directory and its parent a '.env' file. If no file is found, a empty list is returned.
get_config(path = NULL)get_config(path = NULL)
path |
a path from where a config if searched for. Parent directories will be evaluated if no config file is found is the specified directory. If NULL, the current directory will be taken as default. |
## Env file:
Simply create a .env text file on your repository's root directory in the form:
DEBUG=True TEMPLATE_DEBUG=True SECRET_KEY=ARANDOMSECRETKEY DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase PERCENTILE=90 #COMMENTED=42
a list with variables / values from config file. If no config file has been found, an empyt list is returned.
config <- get_config() configconfig <- get_config() config
Return config file path found in a directory
get_config_filename_from_dir(path)get_config_filename_from_dir(path)
path |
The directory were config files are looking for. |
the full path of the config file found. If no config file are found, it returns a 'NULL'.
Get a variable from, in order of priority, environment variable, .env or settings.ini, or default values. Data type can be cast to boolean or integer.
get_var( config = NULL, var_name = NULL, path = NULL, default = structure("UNDEFINED_", class = "UNDEFINED_"), cast = NULL )get_var( config = NULL, var_name = NULL, path = NULL, default = structure("UNDEFINED_", class = "UNDEFINED_"), cast = NULL )
config |
an object returned by the |
var_name |
the variable of interest |
path |
the path from where config files are searched for. If NULL,the current directory will be considered as default. |
default |
a default value |
cast |
Either a function or a type of cast. Currently implemented options are 'integer', 'boolean' or 'float'. |
The value associated to the config variable. The type depends on the cast argument. Default is string.
config <- get_config() get_var(config, "test", default = "yes", cast = "bool")config <- get_config() get_var(config, "test", default = "yes", cast = "bool")
Test if object if of class _UNDEFINED
is_undefined(x)is_undefined(x)
x |
an object. |
a boolean
The file is parsed, and line is expected to have one of the following formats:
VARIABLE=value VARIABLE2="quoted value" VARIABLE3='another quoted variable' # Comment line export EXPORTED="exported variable" export EXPORTED2=another
load_dot_env(fpath = ".env")load_dot_env(fpath = ".env")
fpath |
The path to the '.env' config file |
Load variables defined in the given file, as environment variables.
Detailed specification:
A leading export is ignored, to keep the file
compatible with Unix shells.
No whitespace is allowed right before or after the equal sign, again, to promote compatilibity with Unix shells.
No multi-line variables are supported currently. The file is strictly parsed line by line.
Unlike for Unix shells, unquoted values are not terminated by whitespace.
Comments start with #, without any leading
whitespace. You cannot mix variable definitions and
comments in the same line.
Empty lines (containing whitespace only) are ignored.
It is suggested to keep the file in a form that is parsed the
same way with dotenv and bash (or other shells).
A named list of environment variables, where names are the variable names and values are the variable values
# Load from a file tmp <- tempfile() cat("dotenvexamplefoo=bar\n", file = tmp) load_dot_env(tmp) # Clean up unlink(tmp)# Load from a file tmp <- tempfile() cat("dotenvexamplefoo=bar\n", file = tmp) load_dot_env(tmp) # Clean up unlink(tmp)
Parse environment variables from lines in a .env file
parse_dot_line(lines)parse_dot_line(lines)
lines |
Character vector containing lines from a .env file |
A named list where names are environment variable names and values are their corresponding values
Generate a random string of specified length
random_string(string_length = 6, replace = TRUE)random_string(string_length = 6, replace = TRUE)
string_length |
Integer, length of the string to generate. |
replace |
Bollean, Use replace in the sampling procedure. |
A random string.
Parse config file and remove comment lines
remove_comments(lines)remove_comments(lines)
lines |
A list of config file lines. |
A list of config file lines where comment lines have been filtered out.
Parse config file and remove empty lines
remove_empty_lines(lines)remove_empty_lines(lines)
lines |
A list of config file lines. |
A list of config file lines where empty lines have been filtered out.
Allow the convertion of logical related data to actual Boolean
to_logical(x)to_logical(x)
x |
a vector of boolean compatible values. |
a vector of boolean
to_logical(c("yes", "no"))to_logical(c("yes", "no"))