Title: | Simplify Parameters |
---|---|
Description: | An interface to simplify organizing parameters used in a package, using external configuration files. This attempts to provide a cleaner alternative to options(). |
Authors: | Sahil Seth [aut, cre], Yihui Xie [ctb] (kable from knitr R/table.R) |
Maintainer: | Sahil Seth <[email protected]> |
License: | GPL-2 |
Version: | 0.7.7 |
Built: | 2025-02-07 20:25:42 UTC |
Source: | https://github.com/sahilseth/params |
Checks all the arguments in the parent function and makes sure that none of them are NULL
check_args(ignore, select)
check_args(ignore, select)
ignore |
optionally ignore a few variables for checking [character vector]. |
select |
optionally only check a few variables of the function [character vector]. |
myfunc <- function(verbose = get_opts("verbose"), b = get_opts("b")){ check_args() } set_opts(verbose = 1) ## this will throw an error, suggesting b is not defined properly try(myfunc())
myfunc <- function(verbose = get_opts("verbose"), b = get_opts("b")){ check_args() } set_opts(verbose = 1) ## this will throw an error, suggesting b is not defined properly try(myfunc())
removes special chars from names
fix_column_names(x, char = "_")
fix_column_names(x, char = "_")
x |
a character vector |
char |
replace special characters with this symbol |
Replace special characters in column names of a data.frame
fix_names(x, char = "_")
fix_names(x, char = "_")
x |
a vector of column names |
char |
substitute special char with this. |
make.names
This is a very simple table generator. It is simple by design. It is not intended to replace any other R packages for making tables. This is a trimmed down version of the original kable function in knitr package. Please refer to knitr's kable function for details.
kable( x, format, digits = getOption("digits"), row.names = NA, col.names = colnames(x), align, caption = NULL, escape = TRUE, ... )
kable( x, format, digits = getOption("digits"), row.names = NA, col.names = colnames(x), align, caption = NULL, escape = TRUE, ... )
x |
an R object (typically a matrix or data frame) |
format |
a character string; possible values are |
digits |
the maximum number of digits for numeric columns (passed to
|
row.names |
a logical value indicating whether to include row names; by
default, row names are included if |
col.names |
a character vector of column names to be used in the table |
align |
the alignment of columns: a character vector consisting of
|
caption |
the table caption |
escape |
escape special characters when producing HTML or LaTeX tables |
... |
other arguments (see examples) |
Yihui Xie http://yihui.org
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
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 class 'opts' print(x, ...)
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 class 'opts' print(x, ...)
x |
|
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 |
... |
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 ... |
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 |
## 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}}")
## 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}}")
default opts
opts
opts
An object of class environment
of length 6.
{{variable}}
into their respective valuesThis function is internally called by set_opts and load_opts
parse_opts(lst, envir)
parse_opts(lst, envir)
lst |
a list of configuration options to parse |
envir |
environ used to store objects. Default is a environ object called opts [params:::opts] |
Read/Write sheets (automatically detect the file type and work accordingly)
write_sheet requires version 0.3.1.
tsv, txt, conf, def: assumed to be tab-delimited
csv: assumed to be comma delimited
xlsx: microsoft excel, uses openxlsx to read the sheet. Also, it removes extra columns which often creep into excel files.
read_sheet( x, id_column, start_row = 1, sheet = 1, ext, header = TRUE, verbose = FALSE, ... ) write_sheet(x, file, ext, type = "", ...)
read_sheet( x, id_column, start_row = 1, sheet = 1, ext, header = TRUE, verbose = FALSE, ... ) write_sheet(x, file, ext, type = "", ...)
x |
read: path to a file, to be read. write: a data.frame |
id_column |
all rows which have this column as blank are skipped. See details. |
start_row |
supplied to read.xlsx |
sheet |
supplied to read.xlsx, index or name of the sheet to be read from excel file. See read.xlsx |
ext |
determined using file extension. Specifying will override |
header |
first line is header? See read.table |
verbose |
verbosity level. |
... |
passed onto read.xlsx of openxlsx, read.table or read.csv2 depending on the file type. |
file |
write: output file name. |
type |
in case of writing an xlsx file, should the data.frame to written as excel 'table'. ['table'] |
Note: for excel sheets:
If id_column
is missing, default if first column
If sheet
is missing, it automatically reads the first sheet
Some important default values for tsv and csv files:
stringsAsFactors = FALSE,comment.char = '#', strip.white=TRUE, blank.lines.skip=TRUE
## read a excel sheet sheet = read_sheet(system.file("extdata/example.xlsx", package = "params")) ## read a comma separated sheet csv = read_sheet(system.file("extdata/example.csv", package = "params")) ## read a tab separate sheet tsv = read_sheet(system.file("extdata/example.tsv", package = "params")) # write sheets ------- ## Not run: # throws a R CMD check note - don't run ## write a comma separated sheet write_sheet(sheet, "example.csv") ## write a tab separated sheet write_sheet(sheet, "example.tsv") ## write an excel separated sheet write_sheet(sheet, "example.xlsx") ## End(Not run)
## read a excel sheet sheet = read_sheet(system.file("extdata/example.xlsx", package = "params")) ## read a comma separated sheet csv = read_sheet(system.file("extdata/example.csv", package = "params")) ## read a tab separate sheet tsv = read_sheet(system.file("extdata/example.tsv", package = "params")) # write sheets ------- ## Not run: # throws a R CMD check note - don't run ## write a comma separated sheet write_sheet(sheet, "example.csv") ## write a tab separated sheet write_sheet(sheet, "example.tsv") ## write an excel separated sheet write_sheet(sheet, "example.xlsx") ## End(Not run)