opt_set: Set, Get, and Remove (Package-Specific) Options

Description Usage Arguments Value Examples

View source: R/func_options.R

Description

These are utility functions for setting, getting, and removing package-specific options. The withr package describes the dangers of changing the landscape of R by modifying things like search paths, global options, or working directories. Specifically, they write "If the behavior of other functions differs before and after running your function, you've modified the landscape."

The withr package provides elegant solutions that are more in-line with best practices. However, I found that there options I'd like to use within personal packages, such as jdtools, and I believe it makes sense to modify the R landscape. To avoid potential issues as best as possible, the set of functions here prefix any options with a package name.

This allows users to make some changes to the R landscape but in a more controlled manner that should avoid many conflicts.

Thanks to TJ Mahr, Tan Ho, and Tyler Grant Smith for providing code that helped solve option-setting issues.

Usage

1
2
3
4
5
6
7
8
9
opt_set(option, package)

opt_get(option, package)

opt_ls(package)

opt_rm(option, package)

opt_rm_all(package)

Arguments

option

A named vector or list corresponding to the options and values.

package

A character string corresponding to the package name whose prefix you would like to set.If the package parameter is not supplied, these functions assume you are working on a package and will prefix the options with the name of the package via desc::desc_get(). If you do not pass in a package parameter and are not working on a package, an error will be thrown. Can also set the package parameter globally with options(jdtools.opts_package_name = "THE_PACKAGE_NAME").

Value

Either nothing, when used for option manipulations, or character vectors of existing options.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#' ## Always define package parameter
# List current {jdtools} options
opt_ls("jdtools")

# Set new options
# Either a list
opt_set(list(favorite_color = "black",
             favorite_food = "sushi",
             favorite_pet = "tucker"),
        "jdtools")
# Or a character vector
opt_set(c(favorite_color = "black",
          favorite_food = "sushi",
          favorite_pet = "tucker"),
        "jdtools")

opt_ls("jdtools")

# Either a list
opt_rm(list("favorite_color", "favorite_food"), "jdtools")

# Or a character vector
opt_rm(c("favorite_color", "favorite_food"), "jdtools")

opt_ls("jdtools")

# Can remove all at once in an interactive session:
if (interactive()) opt_rm_all("jdtools")


## Can set global package option to avoid specifying it in the future:
opt_set(c(opts_package_name = "jdtools"), "jdtools")

# List current {jdtools} options
opt_ls()

# Set new options
opt_set(c(favorite_color = "black",
          favorite_food = "sushi",
          favorite_pet = "tucker"))

opt_ls()

# Either a list
opt_rm(list("favorite_color", "favorite_food"))

opt_ls()

# Can remove all at once in an interactive session:
if (interactive()) opt_rm_all("jdtools")

jdtrat/jdtools documentation built on Dec. 20, 2021, 10:05 p.m.