Learn R Programming

params (version 0.7.3)

load_opts: Setting/loading and extracting various options into the environment

Description

  • set_opts(): set options into a custom envir

  • get_opts(): extract options

  • load_opts(): Read a tab delimited file using read_sheet or toml file 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

load_opts(x, check = TRUE, envir = opts, verbose = TRUE, .parse = TRUE, ...)

load_toml(toml, .remove_period = T, envir = envir, verbose = T)

new_opts(envir = new.env())

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

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

# S3 method for opts print(x, ...)

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 separated by comma

toml

load_toml(): instead of a tsv, use toml to load options

.remove_period

load_opts(): remove \. period from option names (and replace with _)

.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 system. 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 don't 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      |

See Also

read_sheet

read_sheet

read_sheet load_opts

Examples

Run this code
# NOT RUN {
## 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}}")

# }

Run the code above in your browser using DataLab