Setting/loading and extracting various options into the environment

Usage

load_opts(x, check = TRUE, envir = opts, verbose = TRUE, .parse = TRUE, ...)
get_opts(x, envir = opts)
set_opts(..., .dots, .parse = TRUE, envir = opts)
## method for class 'opts' print(x, ...)
new_opts(envir = new.env())

Arguments

x
  • get_opts: a character vector of names of options to extract.
  • load_opts: path to a configuration file
check
load_opts(): in case of a configuration file, whether to check if files defined in parameters exists. [TRUE]
envir
environ used to store objects. Default is a environ object called opts [params:::opts]
verbose
load_opts(): Logical variable indicate level of verboseness [TRUE]
.parse
set_opts(), load_opts(): logical, whether to auto-complete {{myvar}} using previously defined options. [TRUE]
...
set_opts(): a named set of variable/value pairs seperated by comma
.dots
set_opts(): A named list, as a alternative to ...

Description

  • set_opts(): set options into a custom envir
  • get_opts(): extract options
  • load_opts(): Read a tab delimted file using read_sheet and load them as options using set_opts
  • new_opts(): create a options manager to be included in a pacakge
  • print.opts(): print pkg options as a pretty table

Details

Integrating params in a package:

create a options manager:

opts_mypkg = new_opts()

The object opts_mypkg is a list of a few functions, which set, fetch and load options (using a isolated environment). Here are a few examples:

Set some options:

opts_mypkg$set(version = '0.1', name = 'mypkg')

Fetch ALL options:

opts_mypkg$get() OR opts_mypkg$get("version") to fetch a specific option.

Loading configuration files:

load_opts() OR opts_pkg$load():

There are cases when options and params are actually paths to scripts or other apps or folders etc. In such cases it might be useful to quickly check if these paths exists on the sytem. As such, load_opts() automatically checks params ending with path|dir|exe (if check=TRUE).

For example, values for variables like mypath, my_path, tool_exe, etc would be check if they exists and a warning would be shown if they dont exist.

Below is a list example options, retrieved via

get_opts():

	|name          |value            |
	|default_regex |(.*)             |
	|my_conf_path  |~/flowr/conf     |
	|my_dir        |path/to/a/folder |
	|my_path       |~/flowr          |
	|my_tool_exe   |/usr/bin/ls      |

Examples

## Set options opts = set_opts(flow_run_path = "~/mypath") #OR opts = set_opts(.dots = list(flow_run_path = "~/mypath")) ## printing options, this is internally called by get_opts() print(opts)
flow_run_path "~/mypath"
## Fetch options get_opts()
|name |value | |:-------------|:----------------| |default_regex |(.*) | |flow_run_path |~/mypath | |my_conf_path |~/flowr/conf | |my_dir |path/to/a/folder | |my_path |~/flowr | |my_tool_exe |/usr/bin/ls | |verbose |1 |
get_opts("flow_run_path")
flow_run_path "~/mypath"
## Load options from a file fl = system.file("conf/params.conf", package = "params") load_opts(fl)
Reading file, using 'V1' as id_column to remove empty rows. Warning message: Seems like these paths do not exist, this may cause issues later: |name |value | |:-----------|:----------------| |my_dir |path/to/a/folder | |my_tool_exe |/usr/bin/ls |
## Create a options manager: opts_mypkg = new_opts() ## this provides three functions opts_mypkg$set(version = '0.1', name = 'mypkg') opts_mypkg$load(fl)
Reading file, using 'V1' as id_column to remove empty rows. Warning message: Seems like these paths do not exist, this may cause issues later: |name |value | |:-----------|:----------------| |my_dir |path/to/a/folder | |my_tool_exe |/usr/bin/ls |
opts_mypkg$get()
|name |value | |:-------------|:----------------| |default_regex |(.*) | |my_conf_path |~/flowr/conf | |my_dir |path/to/a/folder | |my_path |~/flowr | |my_tool_exe |/usr/bin/ls | |name |mypkg | |verbose |2 | |version |0.1 |
## Additionally, one has the options of using braces ({{}}) ## do define nested options: set_opts(first = "John", last = "Doe", full = "{{first}} {{last}}")