2
4
Fork
You've already forked options
0
Simple, Consistent Package Options https://dgkf.codeberg.page/options/
  • R 98.5%
  • SCSS 1.5%
dgkf 3de5a6e8dc
All checks were successful
ci/woodpecker/push/r-pkg-standard Pipeline was successful
ci/woodpecker/tag/r-pkg-standard Pipeline was successful
Fix: null defaults, logical envvar parsing ( #34 )
Closes #33
Closes #32
* Fixed `define_options` when using a `NULL` default value. Previously,
 the `NULL` value would not be recognized and would silently ignore the
 option definition. (@dgkf #34)
* Fixed `envvar_is.logical` (and thereby, `envvar_is_true`, `envvar_is_false`),
 leveraging `as.logical` internally to make the parsing from strings align
 more closely to what one may expect in R. Notably, all lowercase values
 `true` and `false` will now be recognized as their respective logical values.
 (@dgkf #34)
Reviewed-on: #34 
2025年03月28日 21:02:12 +00:00
.github fix: spelling mistake in readme 2024年10月12日 21:08:06 +00:00
.woodpecker test: deploy token setup 2025年02月15日 23:49:19 -05:00
inst/options.example adding NAMESPACE and man files to example package ( #4 ) 2023年08月07日 15:19:58 -04:00
man Feature: add opts_list for interoperability with options(),withr ( #19 ) 2024年09月16日 22:56:31 -04:00
pkgdown Expose get_options_env, fix opts() ( #17 ) 2024年05月11日 19:30:38 -04:00
R Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
tests Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
vignettes adding vignettes; gh actions 2022年12月21日 20:23:34 -05:00
.codecov.yml add codecov.yml file 2024年09月28日 23:40:45 -04:00
.gitignore Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
.Rbuildignore Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
DESCRIPTION Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
LICENSE polish pass 2022年12月20日 23:06:14 -05:00
LICENSE.md polish pass 2022年12月20日 23:06:14 -05:00
NAMESPACE Feature: add opts_list for interoperability with options(),withr ( #19 ) 2024年09月16日 22:56:31 -04:00
NEWS.md Fix: null defaults, logical envvar parsing ( #34 ) 2025年03月28日 21:02:12 +00:00
README.md bump ci 2025年02月20日 11:38:56 -05:00

options

CRAN R CMD check Codecov downloads Matrix Space

Simple, Consistent Package Options

If you've exposed options from a package before, you've inevitably re-written one or more pieces of trivial boilerplate code:

  • Prefixing option names with some sort of package namespace
  • Building your own option documentation
  • Preferentially using a default value, global options or environment variables
  • Parsing of environment variables into useful R data

options aims to make these things easy, without having to copy around boilerplate code.

Quick Start

Defining Options

Define your options using the define_options shorthand. Interlace descriptions and default values to define multiple options at once.

#' @import options
options::define_options(
 "This is an example of how a package author would document their internally
 used options. This option could make the package default to executing
 quietly.",
 quiet = TRUE,
 "Multiple options can be defined, providing default values if a global option
 or environment variable isn't set.",
 second_example = FALSE,
 "Default values are lazily evaluated, so you are free to use package functions
 without worrying about build-time evaluation order",
 lazy_example = fn_not_defined_until_later()
)

When you want more control, you can use define_option to declare all aspects of how your option behaves.

options::define_option(
 option = "concrete_example",
 default = TRUE,
 desc = paste0(
 "Or, if you prefer a more concrete constructor you can define each option ",
 "explicitly."
 ),
 option_name = "mypackage_concrete", # define custom option names
 envvar_name = "MYPACKAGE_CONCRETE", # and custom environment variable names
 envvar_fn = options::envvar_is_true() # and use helpers to handle envvar parsing
)

Documentation

As long as the options have been created as shown above, documenting your options is as easy as adding this small roxygen stub within your package.

#' @eval options::as_roxygen_docs()
NULL

Produces ?mypackage::options

mypackage Options
Description:
 Internally used, package-specific options. All options will
 prioritize R options() values, and fall back to environment
 variables if undefined. If neither the option nor the environment
 variable is set, a default value is used.
Options:
 quiet
 This is an example of how a package author would document their
 internally used options. This option could make the package default to
 executing quietly.
 default:
 TRUE
 option: mypackage.quiet
 envvar: R_MYPACKAGE_QUIET (raw)
...

When your options are used as default values to parameters, you can use the option documentation to populate your function parameter docs.

This is made simple when all of your parameters share the same names as your options.

#' @eval options::as_params()
#' @name options_params
#'
NULL
#' Count to Three
#'
#' @inheritParams option_params
#'
count_to_three <- function(quiet = opt("quiet")) {
 for (i in 1:3) if (!quiet) cat(i, "\n")
}

In situations where you have identically named parameters where you don't want to inherit the option documentation, you can provide their names to as_params to use just a subset of options. You can also reassign documentation for an option to a parameter of a different name.

#' Hello World!
#'
#' @eval options::as_params("silent" = "quiet")
#'
hello <- function(who, silent = opt("quiet")) {
 cat(paste0("Hello, ", who, "!"), "\n")
}

Customizing Behaviors

When using define_option you can set the option_name and envvar_name that will be used directly.

But it can be tedious and typo-prone to write these out for each and every option. Instead, you might consider providing a function that sets the default format for your option and environment variable names.

For this, you can use set_option_name_fn and set_envvar_name_fn, which each accept a function as an argument. This function accepts two arguments, a package name and internal option name, which it uses to produce and return the corresponding global option name or environment variable name.

options::set_option_name_fn(function(package, name) {
 tolower(paste0(package, ".", name))
})
options::set_envvar_name_fn(function(package, name) {
 gsub("[^A-Z0-9]", "_", toupper(paste0(package, "_", name)))
})