Setting/loading and extracting various options into the environment

Share:

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
load_opts(x, check = TRUE, envir = opts, verbose = TRUE, .parse = TRUE,
  ...)

get_opts(x, envir = opts, .use.names = FALSE)

set_opts(..., .dots, .parse = TRUE, envir = opts)

## S3 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

.use.names

get_opts(): The resulting vector should be have names (esp. if length(x) is 1). If length(x)>1, this returns a list.

.dots

set_opts(): A named list, as a alternative to ...

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():

1
2
3
4
5
6
|name          |value            |
|default_regex |(.*)             |
|my_conf_path  |~/flowr/conf     |
|my_dir        |path/to/a/folder |
|my_path       |~/flowr          |
|my_tool_exe   |/usr/bin/ls      |

See Also

read_sheet

read_sheet load_opts

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## 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)

## Fetch options
get_opts()
get_opts("flow_run_path")

## Load options from a file
fl = system.file("conf/params.conf", package = "params")
load_opts(fl)


## Create a options manager:
opts_mypkg = new_opts()
## this provides three functions
opts_mypkg$set(version = '0.1', name = 'mypkg')
opts_mypkg$load(fl)
opts_mypkg$get()

## Additionally, one has the options of using braces ({{}})
## do define nested options:

set_opts(first = "John", last = "Doe", full = "{{first}} {{last}}")