Strict separation of settings from code.
DecoupleR is an attempt to port the excellent python-decouple library into R.
As stated by its original author, python-decouple makes it easy to: - store parameters in ini or .env files; - define comprehensive default values; - properly convert values to the correct data type; - have only one configuration module to rule all your instances.
DecoupleR general behavior have been in order to mimic as much as possible python-decouple, and is tested against python-decouple unit tests.
DecoupleR always searches for Options in this order:
It has to be noted that environment variables have precedence over config files in order to be unix consistent.
The config files are searched for, by default, in the working directory, or in the any other directory provided via the path argument. If no config files, settings.ini or .env is found in the directory, the search will continue in its parent directories.
Parameter value can be retrieve anytime by invoking the DecoupleR::get_var function. If the config parameter is not provided, a config file search will be carried out at each function call.
get_var('R_HOME')
In order to avoid recurrent config file search at each get_var call, the config file search can be carried out once via the get_config function. A list will be returned, containing the parameters from the config file if any.
DecoupleR supports both .ini and .env files.
DecoupleR can read ini files and provide simple interpolation.
Simply create a `settings.ini
in your working directory or in its roots.
[settings]
DEBUG=True
TEMPLATE_DEBUG=%(DEBUG)s
SECRET_KEY=ARANDOMSECRETKEY
DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase
PERCENTILE=90%%
#COMMENTED=42
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
On the above example, all configuration parameters except SECRET_KEY = config('SECRET_KEY')
have a default value to fallback if it does not exist on the .env
file.
If SECRET_KEY
is not present in the .env
, DecoupleR will raise an error.
This fail fast policy helps you avoid chasing misbehaviors when you eventually forget a parameter.
By default, all values returned by DecoupleR
are strings
.
To easily specify a returned type, the get_var
function accepts a cast
argument which receives any callable, that will be used to transform the string value into something else. Some prefedined transformation have also been implementated:
- Integer: int
, integer
- Bolean: bool
, boolean
, logical
- Float: float
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.