collapse-options | R Documentation |
collapse is globally configurable to an extent few packages are: the default value of key function arguments governing the behavior of its algorithms, and the exported namespace, can be adjusted interactively through the set_collapse()
function.
These options are saved in an internal environment called .op
(for safety and performance reasons) visible in the documentation of some functions such as fmean
. The contents of this environment can be accessed using get_collapse()
.
There are also a few options that can be set using options
(retrievable using getOption
). These options mainly affect package startup behavior.
set_collapse(...)
get_collapse(opts = NULL)
... |
either comma separated options, or a single list of options. The available options are:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opts |
character. A vector of options to receive from |
set_collapse()
returns the old content of .op
invisibly as a list. get_collapse()
, if called with only one option, returns the value of the option, and otherwise a list.
options()
"collapse_unused_arg_action"
regulates how generic functions (such as the Fast Statistical Functions) in the package react when an unknown argument is passed to a method. The default action is "warning"
which issues a warning. Other options are "error"
, "message"
or "none"
, whereby the latter enables silent swallowing of such arguments.
"collapse_export_F"
, if set to TRUE
, exports the lead operator F
in the package namespace when loading the package. The operator was exported by default until v1.9.0, but is now hidden inside the package due to too many problems with base::F
. Alternatively, the operator can be accessed using collapse:::F
.
"collapse_nthreads"
, "collapse_na_rm"
, "collapse_sort"
, "collapse_stable_algo"
, "collapse_verbose"
, "collapse_digits"
, "collapse_mask"
and "collapse_remove"
can be set before loading the package to initialize .op
with different defaults (e.g. using an .Rprofile
file). Once loaded, these options have no effect, and users need to use set_collapse()
to change them. See also the Note.
Setting keywords "fast-fun", "fast-stat-fun", "fast-trfm-fun" or "all" with set_collapse(mask = ...)
will also adjust internal optimization flags, e.g. in (f)summarise
and (f)mutate
, so that these functions - and all expressions containing them - receive vectorized execution (see examples of (f)summarise
and (f)mutate
). Users should be aware of expressions like fmutate(mu = sum(var) / lenth(var))
: this usually gets executed by groups, but with these keywords set,this will be vectorized (like fmutate(mu = fsum(var) / lenth(var))
) implying grouped sum divided by overall length. In this case fmutate(mu = base::sum(var) / lenth(var))
needs to be specified to retain the original result.
Note that passing individual functions like set_collapse(mask = "(f)sum")
will not change internal optimization flags for these functions. This is to ensure consistency i.e. you can be either all in (by setting appropriate keywords) or all out when it comes to vectorized stats with basic R names.
Note also that masking does not change documentation links, so you need to look up the f- version of a function to get the right documentation.
A safe way to set options affecting startup behavior is by using a .Rprofile
file in your user or project directory (see also here, the user-level file is located at file.path(Sys.getenv("HOME"), ".Rprofile")
and can be edited using file.edit(Sys.getenv("HOME"), ".Rprofile")
), or by using a .fastverse
configuration file in the project directory.
options("collapse_remove")
does in fact remove functions from the namespace and cannot be reversed by set_collapse(remove = NULL)
once the package is loaded. It is only reversed by re-loading collapse.
Collapse Overview, collapse-package
# Setting new values
oldopts <- set_collapse(nthreads = 2, na.rm = FALSE)
# Getting the values
get_collapse()
get_collapse("nthreads")
# Resetting
set_collapse(oldopts)
rm(oldopts)
## Not run:
## This is a typical working setup I use:
library(fastverse)
# Loading other stats packages with fastverse_extend():
# displays versions, checks conflicts, and installs if unavailable
fastverse_extend(qs, fixest, grf, glmnet, install = TRUE)
# Now setting collapse options with some namespace modification
set_collapse(
nthreads = 4,
sort = FALSE,
mask = c("manip", "helper", "special", "mean", "scale"),
remove = "old"
)
# Final conflicts check (optional)
fastverse_conflicts()
# For some simpler scripts I also use
set_collapse(
nthreads = 4,
sort = FALSE,
mask = "all",
remove = c("old", "between") # I use data.table::between > fbetween
)
# This is now collapse code
mtcars |>
subset(mpg > 12) |>
group_by(cyl) |>
sum()
## End(Not run)
## Changing what happens with unused arguments
oldopts <- options(collapse_unused_arg_action = "message") # default: "warning"
fmean(mtcars$mpg, bla = 1)
# Now nothing happens, same as base R
options(collapse_unused_arg_action = "none")
fmean(mtcars$mpg, bla = 1)
mean(mtcars$mpg, bla = 1)
options(oldopts)
rm(oldopts)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.