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.
opt_set(option, package) opt_get(option, package) opt_ls(package) opt_rm(option, package) opt_rm_all(package)
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 |
Either nothing, when used for option manipulations, or character vectors of existing options.
#' ## Always define package parameter # List current {jdtools} options opt_ls("jdtools")#> named list()# 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")#> $jdtools.favorite_color #> [1] "black" #> #> $jdtools.favorite_food #> [1] "sushi" #> #> $jdtools.favorite_pet #> [1] "tucker" #># 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")#> $jdtools.favorite_pet #> [1] "tucker" #># 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()#> $jdtools.favorite_pet #> [1] "tucker" #> #> $jdtools.opts_package_name #> [1] "jdtools" #># Set new options opt_set(c(favorite_color = "black", favorite_food = "sushi", favorite_pet = "tucker")) opt_ls()#> $jdtools.favorite_color #> [1] "black" #> #> $jdtools.favorite_food #> [1] "sushi" #> #> $jdtools.favorite_pet #> [1] "tucker" #> #> $jdtools.opts_package_name #> [1] "jdtools" #>#> $jdtools.favorite_pet #> [1] "tucker" #> #> $jdtools.opts_package_name #> [1] "jdtools" #>