Title: | Helper Functions for 'mlr3' |
---|---|
Description: | Frequently used helper functions and assertions used in 'mlr3' and its companion packages. Comes with helper functions for functional programming, for printing, to work with 'data.table', as well as some generally useful 'R6' classes. This package also supersedes the package 'BBmisc'. |
Authors: | Michel Lang [cre, aut] , Patrick Schratz [aut] |
Maintainer: | Michel Lang <[email protected]> |
License: | LGPL-3 |
Version: | 0.15.1 |
Built: | 2024-10-06 06:05:13 UTC |
Source: | https://github.com/mlr-org/mlr3misc |
Frequently used helper functions and assertions used in 'mlr3' and its companion packages. Comes with helper functions for functional programming, for printing, to work with 'data.table', as well as some generally useful 'R6' classes. This package also supersedes the package 'BBmisc'.
Maintainer: Michel Lang [email protected] (ORCID)
Authors:
Patrick Schratz [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/mlr-org/mlr3misc/issues
This operator is equivalent to !(x %in% y)
.
x %nin% y
x %nin% y
x |
( |
y |
( |
Convert object to a Callback or a list of Callback.
as_callback(x, ...) ## S3 method for class 'Callback' as_callback(x, clone = FALSE, ...) as_callbacks(x, clone = FALSE, ...) ## S3 method for class ''NULL'' as_callbacks(x, ...) ## S3 method for class 'list' as_callbacks(x, clone = FALSE, ...) ## S3 method for class 'Callback' as_callbacks(x, clone = FALSE, ...)
as_callback(x, ...) ## S3 method for class 'Callback' as_callback(x, clone = FALSE, ...) as_callbacks(x, clone = FALSE, ...) ## S3 method for class ''NULL'' as_callbacks(x, ...) ## S3 method for class 'list' as_callbacks(x, clone = FALSE, ...) ## S3 method for class 'Callback' as_callbacks(x, clone = FALSE, ...)
x |
(any) |
... |
(any) |
clone |
( |
Converts a vector to a factor()
and ensures that levels are
in the order of the provided levels.
as_factor(x, levels, ordered = is.ordered(x))
as_factor(x, levels, ordered = is.ordered(x))
x |
(atomic |
levels |
( |
ordered |
( |
(factor()
).
x = factor(c("a", "b")) y = factor(c("a", "b"), levels = c("b", "a")) # x with the level order of y as_factor(x, levels(y)) # y with the level order of x as_factor(y, levels(x))
x = factor(c("a", "b")) y = factor(c("a", "b"), levels = c("b", "a")) # x with the level order of y as_factor(x, levels(y)) # y with the level order of x as_factor(y, levels(x))
This function is intended to be convert any R object to a short descriptive string,
e.g. in base::print()
functions.
The following rules apply:
if x
is atomic()
with length 0 or 1: printed as-is.
if x
is atomic()
with length greater than 1, x
is collapsed with ","
,
and the resulting string is truncated to trunc_width
characters.
if x
is an expression: converted to character.
Otherwise: the class is printed.
If x
is a list, the above rules are applied (non-recursively) to its elements.
as_short_string(x, width = 30L, num_format = "%.4g")
as_short_string(x, width = 30L, num_format = "%.4g")
x |
( |
width |
( |
num_format |
( |
(character(1)
).
as_short_string(list(a = 1, b = NULL, "foo", c = 1:10))
as_short_string(list(a = 1, b = NULL, "foo", c = 1:10))
Assertions for Callback class.
assert_callback(callback, null_ok = FALSE) assert_callbacks(callbacks)
assert_callback(callback, null_ok = FALSE) assert_callbacks(callbacks)
callback |
(Callback). |
null_ok |
( |
callbacks |
(list of Callback). |
This assertion is intended to be called in active bindings of an
R6::R6Class which does not allow assignment.
If rhs
is not missing, an exception is raised.
assert_ro_binding(rhs)
assert_ro_binding(rhs)
rhs |
(any) |
Nothing.
Calls digest::digest()
using the 'xxhash64' algorithm after applying hash_input
to each object.
To customize the hashing behaviour, you can overwrite hash_input
for specific classes.
For data.table
objects, hash_input
is applied to all columns, so you can overwrite hash_input
for
columns of a specific class.
Objects that don't have a specific method are hashed as is.
calculate_hash(...)
calculate_hash(...)
... |
(any) |
(character(1)
).
calculate_hash(iris, 1, "a")
calculate_hash(iris, 1, "a")
Callbacks allow to customize the behavior of processes in mlr3 packages. The following packages implement callbacks:
CallbackOptimization
in bbotk.
CallbackTuning
in mlr3tuning.
CallbackTorch
in mlr3torch
Callback is an abstract base class.
A subclass inherits from Callback and adds stages as public members.
Names of stages should start with "on_"
.
For each subclass a function should be implemented to create the callback.
For an example on how to implement such a function see callback_optimization()
in bbotk.
Callbacks are executed at stages using the function call_back()
.
A Context defines which information can be accessed from the callback.
id
(character(1)
)
Identifier of the callback.
label
(character(1)
)
Label for this object.
Can be used in tables, plot and text output instead of the ID.
man
(character(1)
)
String in the format [pkg]::[topic]
pointing to a manual page for this object.
Defaults to NA
, but can be set by child classes.
state
(named list()
)
A callback can write data into the state.
new()
Creates a new instance of this R6 class.
Callback$new(id, label = NA_character_, man = NA_character_)
id
(character(1)
)
Identifier for the new instance.
label
(character(1)
)
Label for the new instance.
man
(character(1)
)
String in the format [pkg]::[topic]
pointing to a manual page for this object.
The referenced help package can be opened via method $help()
.
format()
Helper for print outputs.
Callback$format(...)
...
(ignored).
print()
Printer.
Callback$print(...)
...
(ignored).
help()
Opens the corresponding help page referenced by field $man
.
Callback$help()
call()
Call the specific stage for a given context.
Callback$call(stage, context)
stage
(character(1)
)
stage.
context
(Context
)
Context.
clone()
The objects of this class are cloneable with this method.
Callback$clone(deep = FALSE)
deep
Whether to make a deep clone.
library(R6) # implement callback subclass CallbackExample = R6Class("CallbackExample", inherit = mlr3misc::Callback, public = list( on_stage_a = NULL, on_stage_b = NULL, on_stage_c = NULL ) )
library(R6) # implement callback subclass CallbackExample = R6Class("CallbackExample", inherit = mlr3misc::Callback, public = list( on_stage_a = NULL, on_stage_b = NULL, on_stage_c = NULL ) )
Takes a character vector and changes the first letter of each element to uppercase.
capitalize(str)
capitalize(str)
str |
( |
Character vector, same length as str
.
capitalize("foo bar")
capitalize("foo bar")
Wrapper around base::cat()
with a line break.
Elements are converted to character and concatenate with base::paste0()
.
If a vector is passed, elements are collapsed with line breaks.
catn(..., file = "")
catn(..., file = "")
... |
( |
file |
( |
catn(c("Line 1", "Line 2"))
catn(c("Line 1", "Line 2"))
Calls find.package()
to check if the all packages are installed.
check_packages_installed( pkgs, warn = TRUE, msg = "The following packages are required but not installed: %s" )
check_packages_installed( pkgs, warn = TRUE, msg = "The following packages are required but not installed: %s" )
pkgs |
( |
warn |
( |
msg |
( |
(logical()
) named with package names. TRUE
if the respective package is installed, FALSE
otherwise.
check_packages_installed(c("mlr3misc", "foobar"), warn = FALSE) # catch warning tryCatch(check_packages_installed(c("mlr3misc", "foobaaar")), packageNotFoundWarning = function(w) as.character(w))
check_packages_installed(c("mlr3misc", "foobar"), warn = FALSE) # catch warning tryCatch(check_packages_installed(c("mlr3misc", "foobaaar")), packageNotFoundWarning = function(w) as.character(w))
Chunk atomic vectors into parts of roughly equal size.
chunk()
takes a vector length n
and returns an integer with chunk numbers.
chunk_vector()
uses base::split()
and chunk()
to split an atomic vector into chunks.
chunk_vector(x, n_chunks = NULL, chunk_size = NULL, shuffle = TRUE) chunk(n, n_chunks = NULL, chunk_size = NULL, shuffle = TRUE)
chunk_vector(x, n_chunks = NULL, chunk_size = NULL, shuffle = TRUE) chunk(n, n_chunks = NULL, chunk_size = NULL, shuffle = TRUE)
x |
( |
n_chunks |
( |
chunk_size |
( |
shuffle |
( |
n |
( |
chunk()
returns a integer()
of chunk indices,
chunk_vector()
a list()
of integer
vectors.
x = 1:11 ch = chunk(length(x), n_chunks = 2) table(ch) split(x, ch) chunk_vector(x, n_chunks = 2) chunk_vector(x, n_chunks = 3, shuffle = TRUE)
x = 1:11 ch = chunk(length(x), n_chunks = 2) table(ch) split(x, ch) chunk_vector(x, n_chunks = 2) chunk_vector(x, n_chunks = 3, shuffle = TRUE)
Functions to retrieve callbacks from mlr_callbacks and set parameters in one go.
clbk(.key, ...) clbks(.keys)
clbk(.key, ...) clbks(.keys)
.key |
( |
... |
(named |
.keys |
( |
Callback call_back
map
-like functions, similar to the ones implemented in purrr:
map()
returns the results of .f
applied to .x
as list.
If .f
is not a function, map
will call [[
on all elements of .x
using the value of .f
as index.
imap()
applies .f
to each value of .x
(passed as first argument) and its name (passed as second argument).
If .x
does not have names, a sequence along .x
is passed as second argument instead.
pmap()
expects .x
to be a list of vectors of equal length, and then applies .f
to the first element of
each vector of .x
, then the second element of .x
, and so on.
map_if()
applies .f
to each element of .x
where the predicate .p
evaluates to TRUE
.
map_at()
applies .f
to each element of .x
referenced by .at
. All other elements remain unchanged.
keep()
keeps those elements of .x
where predicate .p
evaluates to TRUE
.
discard()
discards those elements of .x
where predicate .p
evaluates to TRUE
.
every()
is TRUE
if predicate .p
evaluates to TRUE
for each .x
.
some()
is TRUE
if predicate .p
evaluates to TRUE
for at least one .x
.
detect()
returns the first element where predicate .p
evaluates to TRUE
.
walk()
, iwalk()
and pwalk()
are the counterparts to map()
, imap()
and pmap()
, but
just visit (or change by reference) the elements of .x
. They return input .x
invisibly.
Additionally, the functions map()
, imap()
and pmap()
have type-safe variants with the following suffixes:
*_lgl()
returns a logical(length(.x))
.
*_int()
returns a integer(length(.x))
.
*_dbl()
returns a double(length(.x))
.
*_chr()
returns a character(length(.x))
.
*_br()
returns an object where the results of .f
are put together with base::rbind()
.
*_bc()
returns an object where the results of .f
are put together with base::cbind()
.
*_dtr()
returns a data.table::data.table()
where the results of .f
are put together
in an base::rbind()
fashion.
*_dtc()
returns a data.table::data.table()
where the results of .f
are put
together in an base::cbind()
fashion.
map(.x, .f, ...) map_lgl(.x, .f, ...) map_int(.x, .f, ...) map_dbl(.x, .f, ...) map_chr(.x, .f, ...) map_br(.x, .f, ...) map_bc(.x, .f, ...) map_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) map_dtc(.x, .f, ...) pmap(.x, .f, ...) pmap_lgl(.x, .f, ...) pmap_int(.x, .f, ...) pmap_dbl(.x, .f, ...) pmap_chr(.x, .f, ...) pmap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) pmap_dtc(.x, .f, ...) imap(.x, .f, ...) imap_lgl(.x, .f, ...) imap_int(.x, .f, ...) imap_dbl(.x, .f, ...) imap_chr(.x, .f, ...) imap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) imap_dtc(.x, .f, ...) keep(.x, .f, ...) discard(.x, .p, ...) map_if(.x, .p, .f, ...) ## Default S3 method: map_if(.x, .p, .f, ...) map_at(.x, .at, .f, ...) every(.x, .p, ...) some(.x, .p, ...) detect(.x, .p, ...) walk(.x, .f, ...) iwalk(.x, .f, ...) pwalk(.x, .f, ...)
map(.x, .f, ...) map_lgl(.x, .f, ...) map_int(.x, .f, ...) map_dbl(.x, .f, ...) map_chr(.x, .f, ...) map_br(.x, .f, ...) map_bc(.x, .f, ...) map_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) map_dtc(.x, .f, ...) pmap(.x, .f, ...) pmap_lgl(.x, .f, ...) pmap_int(.x, .f, ...) pmap_dbl(.x, .f, ...) pmap_chr(.x, .f, ...) pmap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) pmap_dtc(.x, .f, ...) imap(.x, .f, ...) imap_lgl(.x, .f, ...) imap_int(.x, .f, ...) imap_dbl(.x, .f, ...) imap_chr(.x, .f, ...) imap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL) imap_dtc(.x, .f, ...) keep(.x, .f, ...) discard(.x, .p, ...) map_if(.x, .p, .f, ...) ## Default S3 method: map_if(.x, .p, .f, ...) map_at(.x, .at, .f, ...) every(.x, .p, ...) some(.x, .p, ...) detect(.x, .p, ...) walk(.x, .f, ...) iwalk(.x, .f, ...) pwalk(.x, .f, ...)
.x |
( |
.f |
( |
... |
( |
.fill |
( |
.idcol |
( |
.p |
( |
.at |
( |
Composes two or more functions into a single function. The returned function calls all provided functions in reverse order: The return value of the last function servers as input for the next to last function, and so on.
compose(...)
compose(...)
... |
( |
(function()
) which calls the functions provided via ...
in reverse order.
f = compose(function(x) x + 1, function(x) x / 2) f(10)
f = compose(function(x) x + 1, function(x) x / 2) f(10)
Computes the mode (most frequent value) of an atomic vector.
compute_mode(x, ties_method = "random", na_rm = TRUE)
compute_mode(x, ties_method = "random", na_rm = TRUE)
x |
( |
ties_method |
( |
na_rm |
( |
(vector(1)
): mode value.
compute_mode(c(1, 1, 1, 2, 2, 2, 3)) compute_mode(c(1, 1, 1, 2, 2, 2, 3), ties_method = "last") compute_mode(c(1, 1, 1, 2, 2, 2, 3), ties_method = "random")
compute_mode(c(1, 1, 1, 2, 2, 2, 3)) compute_mode(c(1, 1, 1, 2, 2, 2, 3), ties_method = "last") compute_mode(c(1, 1, 1, 2, 2, 2, 3), ties_method = "random")
Context objects allow Callback objects to access and modify data. The following packages implement context subclasses:
ContextOptimization
in bbotk.
ContextEval
in mlr3tuning.
ContextTorch
in mlr3torch
Context is an abstract base class. A subclass inherits from Context. Data is stored in public fields. Access to the data can be restricted with active bindings (see example).
id
(character(1)
)
Identifier of the object.
Used in tables, plot and text output.
label
(character(1)
)
Label for this object.
Can be used in tables, plot and text output instead of the ID.
new()
Creates a new instance of this R6 class.
Context$new(id, label = NA_character_)
id
(character(1)
)
Identifier for the new instance.
label
(character(1)
)
Label for the new instance.
format()
Format object as simple string.
Context$format(...)
...
(ignored).
print()
Print object.
Context$print()
clone()
The objects of this class are cloneable with this method.
Context$clone(deep = FALSE)
deep
Whether to make a deep clone.
library(data.table) library(R6) # data table with column x an y data = data.table(x = runif(10), y = sample(c("A", "B"), 10, replace = TRUE)) # context only allows to access column y ContextExample = R6Class("ContextExample", inherit = Context, public = list( data = NULL, initialize = function(data) { self$data = data } ), active = list( y = function(rhs) { if (missing(rhs)) return(self$data$y) self$data$y = rhs } ) ) context = ContextExample$new(data) # retrieve content of column y context$y # change content of column y to "C" context$y = "C"
library(data.table) library(R6) # data table with column x an y data = data.table(x = runif(10), y = sample(c("A", "B"), 10, replace = TRUE)) # context only allows to access column y ContextExample = R6Class("ContextExample", inherit = Context, public = list( data = NULL, initialize = function(data) { self$data = data } ), active = list( y = function(rhs) { if (missing(rhs)) return(self$data$y) self$data$y = rhs } ) ) context = ContextExample$new(data) # retrieve content of column y context$y # change content of column y to "C" context$y = "C"
Same as sum(is.na(x))
, but without the allocation.
count_missing(x)
count_missing(x)
x |
|
(integer(1)
) number of missing values.
count_missing(c(1, 2, NA, 4, NA))
count_missing(c(1, 2, NA, 4, NA))
Put a function in a "lean" environment that does not carry unnecessary baggage with it (e.g. references to datasets).
crate(.fn, ..., .parent = topenv(), .compile = TRUE)
crate(.fn, ..., .parent = topenv(), .compile = TRUE)
.fn |
( |
... |
(any) |
.parent |
( |
.compile |
( |
meta_f = function(z) { x = 1 y = 2 crate(function() { c(x, y, z) }, x) } x = 100 y = 200 z = 300 f = meta_f(1) f()
meta_f = function(z) { x = 1 y = 2 crate(function() { c(x, y, z) }, x) } x = 100 y = 200 z = 300 f = meta_f(1) f()
A safe version of data.table::CJ()
in case a column is called
sorted
or unique
.
cross_join(dots, sorted = TRUE, unique = FALSE)
cross_join(dots, sorted = TRUE, unique = FALSE)
dots |
(named |
sorted |
( |
unique |
( |
cross_join(dots = list(sorted = 1:3, b = letters[1:2]))
cross_join(dots = list(sorted = 1:3, b = letters[1:2]))
A key-value store for R6::R6 objects. On retrieval of an object, the following applies:
If the object is a R6ClassGenerator
, it is initialized with new()
.
If the object is a function, it is called and must return an instance of a R6::R6 object.
If the object is an instance of a R6 class, it is returned as-is.
Default argument required for construction can be stored alongside their constructors by passing them to $add()
.
as.data.table(d)
Dictionary -> data.table::data.table()
Converts the dictionary to a data.table::data.table()
.
items
(environment()
)
Stores the items of the dictionary
new()
Construct a new Dictionary.
Dictionary$new()
format()
Format object as simple string.
Dictionary$format(...)
...
(ignored).
print()
Print object.
Dictionary$print()
keys()
Returns all keys which comply to the regular expression pattern
.
If pattern
is NULL
(default), all keys are returned.
Dictionary$keys(pattern = NULL)
pattern
(character(1)
).
character()
of keys.
has()
Returns a logical vector with TRUE
at its i-th position if the i-th key exists.
Dictionary$has(keys)
keys
(character()
).
logical()
.
get()
Retrieves object with key key
from the dictionary.
Additional arguments must be named and are passed to the constructor of the stored object.
Dictionary$get(key, ..., .prototype = FALSE)
key
(character(1)
).
...
(any
)
Passed down to constructor.
.prototype
(logical(1)
)
Whether to construct a prototype object.
Object with corresponding key.
mget()
Returns objects with keys keys
in a list named with keys
.
Additional arguments must be named and are passed to the constructors of the stored objects.
Dictionary$mget(keys, ...)
keys
(character()
).
...
(any
)
Passed down to constructor.
Named list()
of objects with corresponding keys.
add()
Adds object value
to the dictionary with key key
, potentially overwriting a previously stored item.
Additional arguments in ...
must be named and are passed as default arguments to value
during construction.
Dictionary$add(key, value, ..., .prototype_args = list())
key
(character(1)
).
value
(any
).
...
(any
)
Passed down to constructor.
.prototype_args
(list()
)
List of arguments to construct a prototype object.
Can be used when objects have construction arguments without defaults.
Dictionary
.
remove()
Removes objects with from the dictionary.
Dictionary$remove(keys)
keys
(character()
)
Keys of objects to remove.
Dictionary
.
prototype_args()
Returns the arguments required to construct a simple prototype of the object.
Dictionary$prototype_args(key)
key
(character(1)
)
Key of object to query for required arguments.
list()
of prototype arguments
clone()
The objects of this class are cloneable with this method.
Dictionary$clone(deep = FALSE)
deep
Whether to make a deep clone.
library(R6) item1 = R6Class("Item", public = list(x = 1)) item2 = R6Class("Item", public = list(x = 2)) d = Dictionary$new() d$add("a", item1) d$add("b", item2) d$add("c", item1$new()) d$keys() d$get("a") d$mget(c("a", "b"))
library(R6) item1 = R6Class("Item", public = list(x = 1)) item2 = R6Class("Item", public = list(x = 2)) d = Dictionary$new() d$add("a", item1) d$add("b", item2) d$add("c", item1$new()) d$keys() d$get("a") d$mget(c("a", "b"))
Given a Dictionary, retrieve objects with provided keys.
dictionary_sugar_get()
to retrieve a single object with key .key
.
dictionary_sugar_mget()
to retrieve a list of objects with keys .keys
.
dictionary_sugar()
is deprecated in favor of dictionary_sugar_get()
.
If .key
or .keys
is missing, the dictionary itself is returned.
Arguments in ...
must be named and are consumed in the following order:
All arguments whose names match the name of an argument of the constructor
are passed to the $get()
method of the Dictionary for construction.
All arguments whose names match the name of a parameter of the paradox::ParamSet of the
constructed object are set as parameters. If there is no paradox::ParamSet in obj$param_set
, this
step is skipped.
All remaining arguments are assumed to be regular fields of the constructed R6 instance, and
are assigned via <-
.
dictionary_sugar_get(dict, .key, ...) dictionary_sugar(dict, .key, ...) dictionary_sugar_mget(dict, .keys, ...)
dictionary_sugar_get(dict, .key, ...) dictionary_sugar(dict, .key, ...) dictionary_sugar_mget(dict, .keys, ...)
dict |
(Dictionary). |
.key |
( |
... |
( |
.keys |
( |
library(R6) item = R6Class("Item", public = list(x = 0)) d = Dictionary$new() d$add("key", item) dictionary_sugar_get(d, "key", x = 2)
library(R6) item = R6Class("Item", public = list(x = 0)) d = Dictionary$new() d$add("key", item) dictionary_sugar_get(d, "key", x = 2)
Given a Dictionary, retrieve objects with provided keys.
dictionary_sugar_get_safe()
to retrieve a single object with key .key
.
dictionary_sugar_mget_safe()
to retrieve a list of objects with keys .keys
.
If .key
or .keys
is missing, the dictionary itself is returned.
Dictionary getters without the _safe
suffix are discouraged as this sometimes caused unintended partial
argument matching.
Arguments in ...
must be named and are consumed in the following order:
All arguments whose names match the name of an argument of the constructor
are passed to the $get()
method of the Dictionary for construction.
All arguments whose names match the name of a parameter of the paradox::ParamSet of the
constructed object are set as parameters. If there is no paradox::ParamSet in obj$param_set
, this
step is skipped.
All remaining arguments are assumed to be regular fields of the constructed R6 instance, and
are assigned via <-
.
dictionary_sugar_get_safe(.dict, .key, ...) dictionary_sugar_mget_safe(.dict, .keys, ...)
dictionary_sugar_get_safe(.dict, .key, ...) dictionary_sugar_mget_safe(.dict, .keys, ...)
.dict |
(Dictionary) |
.key |
( |
... |
( |
.keys |
( |
library(R6) item = R6Class("Item", public = list(x = 0)) d = Dictionary$new() d$add("key", item) dictionary_sugar_get_safe(d, "key", x = 2)
library(R6) item = R6Class("Item", public = list(x = 0)) d = Dictionary$new() d$add("key", item) dictionary_sugar_get_safe(d, "key", x = 2)
Covenience wrapper around dictionary_sugar_get and dictionary_sugar_mget to allow easier avoidance of of ID
clashes which is useful when the same object is used multiple times and the ids have to be unique.
Let <key>
be the key of the object to retrieve. When passing the <key>_<n>
to this
function, where <n>
is any natural numer, the object with key <key>
is retrieved and the
suffix _<n>
is appended to the id after the object is constructed.
dictionary_sugar_inc_get(dict, .key, ...) dictionary_sugar_inc_mget(dict, .keys, ...)
dictionary_sugar_inc_get(dict, .key, ...) dictionary_sugar_inc_mget(dict, .keys, ...)
dict |
(Dictionary) |
.key |
( |
... |
(any) |
.keys |
( |
An element from the dictionary.
d = Dictionary$new() d$add("a", R6::R6Class("A", public = list(id = "a"))) d$add("b", R6::R6Class("B", public = list(id = "c"))) obj1 = dictionary_sugar_inc_get(d, "a_1") obj1$id obj2 = dictionary_sugar_inc_get(d, "b_1") obj2$id objs = dictionary_sugar_inc_mget(d, c("a_10", "b_2")) map(objs, "id")
d = Dictionary$new() d$add("a", R6::R6Class("A", public = list(id = "a"))) d$add("b", R6::R6Class("B", public = list(id = "c"))) obj1 = dictionary_sugar_inc_get(d, "a_1") obj1$id obj2 = dictionary_sugar_inc_get(d, "b_1") obj2$id objs = dictionary_sugar_inc_mget(d, c("a_10", "b_2")) map(objs, "id")
Covenience wrapper around dictionary_sugar_get_safe and dictionary_sugar_mget_safe to allow easier avoidance of of ID
clashes which is useful when the same object is used multiple times and the ids have to be unique.
Let <key>
be the key of the object to retrieve. When passing the <key>_<n>
to this
function, where <n>
is any natural numer, the object with key <key>
is retrieved and the
suffix _<n>
is appended to the id after the object is constructed.
dictionary_sugar_inc_get_safe(.dict, .key, ...) dictionary_sugar_inc_mget_safe(.dict, .keys, ...)
dictionary_sugar_inc_get_safe(.dict, .key, ...) dictionary_sugar_inc_mget_safe(.dict, .keys, ...)
.dict |
(Dictionary) |
.key |
( |
... |
(any) |
.keys |
( |
An element from the dictionary.
d = Dictionary$new() d$add("a", R6::R6Class("A", public = list(id = "a"))) d$add("b", R6::R6Class("B", public = list(id = "c"))) obj1 = dictionary_sugar_inc_get_safe(d, "a_1") obj1$id obj2 = dictionary_sugar_inc_get_safe(d, "b_1") obj2$id objs = dictionary_sugar_inc_mget_safe(d, c("a_10", "b_2")) map(objs, "id")
d = Dictionary$new() d$add("a", R6::R6Class("A", public = list(id = "a"))) d$add("b", R6::R6Class("B", public = list(id = "c"))) obj1 = dictionary_sugar_inc_get_safe(d, "a_1") obj1$id obj2 = dictionary_sugar_inc_get_safe(d, "b_1") obj2$id objs = dictionary_sugar_inc_mget_safe(d, c("a_10", "b_2")) map(objs, "id")
Helps to suggest alternatives from a list of strings, based on the string similarity in utils::adist()
.
did_you_mean(str, candidates)
did_you_mean(str, candidates)
str |
( |
candidates |
( |
(character(1)
). Either a phrase suggesting one or more candidates from candidates
,
or an empty string if no close match is found.
did_you_mean("yep", c("yes", "no"))
did_you_mean("yep", c("yes", "no"))
Extracts the distinct values of an atomic vector, with the possibility to drop levels and remove missing values.
distinct_values(x, drop = TRUE, na_rm = TRUE)
distinct_values(x, drop = TRUE, na_rm = TRUE)
x |
(atomic |
drop |
:: |
na_rm |
:: |
(atomic vector()
) with distinct values in no particular order.
# for factors: x = factor(c(letters[1:2], NA), levels = letters[1:3]) distinct_values(x) distinct_values(x, na_rm = FALSE) distinct_values(x, drop = FALSE) distinct_values(x, drop = FALSE, na_rm = FALSE) # for logicals: distinct_values(TRUE, drop = FALSE) # for numerics: distinct_values(sample(1:3, 10, replace = TRUE))
# for factors: x = factor(c(letters[1:2], NA), levels = letters[1:3]) distinct_values(x) distinct_values(x, na_rm = FALSE) distinct_values(x, drop = FALSE) distinct_values(x, drop = FALSE, na_rm = FALSE) # for logicals: distinct_values(TRUE, drop = FALSE) # for numerics: distinct_values(sample(1:3, 10, replace = TRUE))
Evaluates a function while both recording an output log and measuring the elapsed time. There are currently three different modes implemented to encapsulate a function call:
"none"
: Just runs the call in the current session and measures the elapsed time.
Does not keep a log, output is printed directly to the console.
Works well together with traceback()
.
"try"
: Similar to "none"
, but catches error. Output is printed to the console and
not logged.
"evaluate"
: Uses the package evaluate to call the function, measure time and do the logging.
"callr"
: Uses the package callr to call the function, measure time and do the logging.
This encapsulation spawns a separate R session in which the function is called.
While this comes with a considerable overhead, it also guards your session from being teared down by segfaults.
encapsulate( method, .f, .args = list(), .opts = list(), .pkgs = character(), .seed = NA_integer_, .timeout = Inf )
encapsulate( method, .f, .args = list(), .opts = list(), .pkgs = character(), .seed = NA_integer_, .timeout = Inf )
method |
( |
.f |
( |
.args |
( |
.opts |
(named |
.pkgs |
( |
.seed |
( |
.timeout |
( |
(named list()
) with three fields:
"result"
: the return value of .f
"elapsed"
: elapsed time in seconds. Measured as proc.time()
difference before/after the function call.
"log"
: data.table()
with columns "class"
(ordered factor with levels "output"
, "warning"
and "error"
) and "message"
(character()
).
f = function(n) { message("hi from f") if (n > 5) { stop("n must be <= 5") } runif(n) } encapsulate("none", f, list(n = 1), .seed = 1) if (requireNamespace("evaluate", quietly = TRUE)) { encapsulate("evaluate", f, list(n = 1), .seed = 1) } if (requireNamespace("callr", quietly = TRUE)) { encapsulate("callr", f, list(n = 1), .seed = 1) }
f = function(n) { message("hi from f") if (n > 5) { stop("n must be <= 5") } runif(n) } encapsulate("none", f, list(n = 1), .seed = 1) if (requireNamespace("evaluate", quietly = TRUE)) { encapsulate("evaluate", f, list(n = 1), .seed = 1) } if (requireNamespace("callr", quietly = TRUE)) { encapsulate("callr", f, list(n = 1), .seed = 1) }
enframe()
returns a data.table::data.table()
with two columns:
The names of x
(or seq_along(x)
if unnamed) and the values of x
.
deframe()
converts a two-column data.frame to a named vector.
If the data.frame only has a single column, an unnamed vector is returned.
enframe(x, name = "name", value = "value") deframe(x)
enframe(x, name = "name", value = "value") deframe(x)
x |
( |
name |
( |
value |
( |
data.table::data.table()
or named vector
.
x = 1:3 enframe(x) x = set_names(1:3, letters[1:3]) enframe(x, value = "x_values")
x = 1:3 enframe(x) x = set_names(1:3, letters[1:3]) enframe(x, value = "x_values")
Given a formula()
f
, returns all variables used on the left-hand side and
right-hand side of the formula.
extract_vars(f)
extract_vars(f)
f |
( |
(list()
) with elements "lhs"
and "rhs"
, both character()
.
extract_vars(Species ~ Sepal.Width + Sepal.Length) extract_vars(Species ~ .)
extract_vars(Species ~ Sepal.Width + Sepal.Length) extract_vars(Species ~ .)
Operates on a named list of bibentry()
entries and formats them nicely for
documentation with roxygen2.
format_bib()
is intended to be called in the @references
section and
prints the complete entry using toRd()
.
cite_bib()
returns the family name of the first author (if available, falling back
to the complete author name if not applicable) and the year in format
"[LastName] (YYYY)"
.
format_bib(..., bibentries = NULL, envir = parent.frame()) cite_bib(..., bibentries = NULL, envir = parent.frame())
format_bib(..., bibentries = NULL, envir = parent.frame()) cite_bib(..., bibentries = NULL, envir = parent.frame())
... |
( |
bibentries |
(named |
envir |
( |
(character(1)
).
bibentries = list(checkmate = citation("checkmate"), R = citation()) format_bib("checkmate") format_bib("R") cite_bib("checkmate") cite_bib("checkmate", "R")
bibentries = list(checkmate = citation("checkmate"), R = citation()) format_bib("checkmate") format_bib("R") cite_bib("checkmate") cite_bib("checkmate", "R")
Given the left-hand side and right-hand side as character vectors, generates a new
stats::formula()
.
formulate(lhs = character(), rhs = character(), env = NULL, quote = "right")
formulate(lhs = character(), rhs = character(), env = NULL, quote = "right")
lhs |
( |
rhs |
( |
env |
( |
quote |
( |
formulate("Species", c("Sepal.Length", "Sepal.Width")) formulate(rhs = c("Sepal.Length", "Sepal.Width"))
formulate("Species", c("Sepal.Length", "Sepal.Width")) formulate(rhs = c("Sepal.Length", "Sepal.Width"))
Provides access to the private members of R6::R6Class objects.
get_private(x)
get_private(x)
x |
(any) |
environment()
of private members, or NULL
if x
is not an R6 object.
library(R6) item = R6Class("Item", private = list(x = 1))$new() get_private(item)$x
library(R6) item = R6Class("Item", private = list(x = 1))$new() get_private(item)$x
Convenience function to assign a value to a private field of an R6::R6Class instance.
get_private(x, which) <- value
get_private(x, which) <- value
x |
(any) |
which |
(character(1)) |
value |
(any) |
The R6 instance x, modified in-place. If it is not an R6 instance, NULL is returned.
library(R6) item = R6Class("Item", private = list(x = 1))$new() get_private(item)$x get_private(item, "x") = 2L get_private(item)$x
library(R6) item = R6Class("Item", private = list(x = 1))$new() get_private(item)$x get_private(item, "x") = 2L get_private(item)$x
Retrieves the current random seed (.Random.seed
in the global environment),
and initializes the RNG first, if necessary.
get_seed()
get_seed()
integer()
. Depends on the base::RNGkind()
.
str(get_seed())
str(get_seed())
Simply checks if a list contains a given object.
NB1: Objects are compared with identity.
NB2: Only use this on lists with complex objects, for simpler structures there are faster operations.
NB3: Clones of R6 objects are not detected.
has_element(.x, .y)
has_element(.x, .y)
.x |
( |
.y |
( |
has_element(list(1, 2, 3), 1)
has_element(list(1, 2, 3), 1)
Returns the part of an object to be used to calculate its hash.
hash_input(x) ## S3 method for class ''function'' hash_input(x) ## S3 method for class 'data.table' hash_input(x) ## Default S3 method: hash_input(x)
hash_input(x) ## S3 method for class ''function'' hash_input(x) ## S3 method for class 'data.table' hash_input(x) ## Default S3 method: hash_input(x)
x |
(any) |
hash_input(`function`)
: The formals and the body are returned in a list()
.
This ensures that the bytecode or parent environment are not included.
in the hash.
hash_input(data.table)
: The data.table is converted to a regular list and hash_input()
is applied to all elements.
The conversion to a list ensures that keys and indices are not included in the hash.
hash_input(default)
: Returns the object as is.
None.
ids(xs)
ids(xs)
xs |
( |
(character()
).
xs = list(a = list(id = "foo", a = 1), bar = list(id = "bar", a = 2)) ids(xs)
xs = list(a = list(id = "foo", a = 1), bar = list(id = "bar", a = 2)) ids(xs)
Insert elements from y
into x
by name, or remove elements from x
by name.
Works for vectors, lists, environments and data frames and data tables.
Objects with reference semantic (environment()
and data.table::data.table()
) might be modified in-place.
insert_named(x, y) ## S3 method for class ''NULL'' insert_named(x, y) ## Default S3 method: insert_named(x, y) ## S3 method for class 'environment' insert_named(x, y) ## S3 method for class 'data.frame' insert_named(x, y) ## S3 method for class 'data.table' insert_named(x, y) remove_named(x, nn) ## S3 method for class 'environment' remove_named(x, nn) ## S3 method for class 'data.frame' remove_named(x, nn) ## S3 method for class 'data.table' remove_named(x, nn)
insert_named(x, y) ## S3 method for class ''NULL'' insert_named(x, y) ## Default S3 method: insert_named(x, y) ## S3 method for class 'environment' insert_named(x, y) ## S3 method for class 'data.frame' insert_named(x, y) ## S3 method for class 'data.table' insert_named(x, y) remove_named(x, nn) ## S3 method for class 'environment' remove_named(x, nn) ## S3 method for class 'data.frame' remove_named(x, nn) ## S3 method for class 'data.table' remove_named(x, nn)
x |
( |
y |
( |
nn |
( |
Modified object.
x = list(a = 1, b = 2) insert_named(x, list(b = 3, c = 4)) remove_named(x, "b")
x = list(a = 1, b = 2) insert_named(x, list(b = 3, c = 4)) remove_named(x, "b")
An alternative interface for do.call()
, similar to the deprecated function in purrr.
This function tries hard to not evaluate the passed arguments too eagerly which is
important when working with large R objects.
It is recommended to pass all arguments named in order to not rely on positional argument matching.
invoke( .f, ..., .args = list(), .opts = list(), .seed = NA_integer_, .timeout = Inf )
invoke( .f, ..., .args = list(), .opts = list(), .seed = NA_integer_, .timeout = Inf )
.f |
( |
... |
( |
.args |
( |
.opts |
(named |
.seed |
( |
.timeout |
( |
invoke(mean, .args = list(x = 1:10)) invoke(mean, na.rm = TRUE, .args = list(1:10))
invoke(mean, .args = list(x = 1:10)) invoke(mean, na.rm = TRUE, .args = list(1:10))
Check for a Single Scalar Value
is_scalar_na(x)
is_scalar_na(x)
x |
( |
(logical(1)
).
Filters vector x
to only keep elements which are in bounds [lower, upper]
.
This is equivalent to the following, but tries to avoid unnecessary allocations:
x[!is.na(x) & x >= lower & x <= upper]
Currently only works for integer x
.
keep_in_bounds(x, lower, upper)
keep_in_bounds(x, lower, upper)
x |
( |
lower |
( |
upper |
( |
(integer()) with only values in [lower, upper]
.
keep_in_bounds(sample(20), 5, 10)
keep_in_bounds(sample(20), 5, 10)
leanify_r6
moves the content of an R6::R6Class
's functions to an environment,
usually the package's namespace, to save space during serialization of R6 objects.
leanify_package
move all methods of all R6 Classes to an environment.
The function in the class (i.e. the object generator) is replaced by a stump function that does nothing except calling the original function that now resides somewhere else.
It is possible to call this function after the definition of an R6::R6
class inside a package, but it is preferred to use leanify_package()
to just leanify all R6::R6 classes inside a package.
leanify_r6(cls, env = cls$parent_env) leanify_package(pkg_env = parent.frame(), skip_if = function(x) FALSE)
leanify_r6(cls, env = cls$parent_env) leanify_package(pkg_env = parent.frame(), skip_if = function(x) FALSE)
cls |
(R6::R6Class) |
env |
( |
pkg_env |
:: |
skip_if |
:: |
NULL
.
Loads a data set with name id
from package package
and returns it.
If the package is not installed, an error with condition "packageNotFoundError" is raised.
The name of the missing packages is stored in the condition as packages
.
load_dataset(id, package, keep_rownames = FALSE)
load_dataset(id, package, keep_rownames = FALSE)
id |
( |
package |
( |
keep_rownames |
( |
head(load_dataset("iris", "datasets"))
head(load_dataset("iris", "datasets"))
Replaces all values in x
which match old
with values in new
.
Values are matched with base::match()
.
map_values(x, old, new)
map_values(x, old, new)
x |
( |
old |
( |
new |
( |
(vector()
) of the same length as x
.
x = letters[1:5] # replace all "b" with "_b_", and all "c" with "_c_" old = c("b", "c") new = c("_b_", "_c_") map_values(x, old, new)
x = letters[1:5] # replace all "b" with "_b_", and all "c" with "_c_" old = c("b", "c") new = c("_b_", "_c_") map_values(x, old, new)
A simple Dictionary storing objects of class Callback.
Each callback has an associated help page, see mlr_callbacks_[id]
.
This dictionary can get populated with additional callbacks by add-on packages.
As a convention, the key should start with the name of the package, i.e. package.callback
.
For a more convenient way to retrieve and construct learners, see clbk()
/clbks()
.
mlr_callbacks
mlr_callbacks
An object of class DictionaryCallbacks
(inherits from Dictionary
, R6
) of length 13.
Modifies elements of a vector selectively, similar to the functions in purrr.
modify_if()
applies a predicate function .p
to all elements of .x
and applies .f
to those elements of .x
where .p
evaluates to TRUE
.
modify_at()
applies .f
to those elements of .x
selected via .at
.
modify_if(.x, .p, .f, ...) modify_at(.x, .at, .f, ...)
modify_if(.x, .p, .f, ...) modify_at(.x, .at, .f, ...)
.x |
( |
.p |
( |
.f |
( |
... |
( |
.at |
(( |
x = modify_if(iris, is.factor, as.character) str(x) x = modify_at(iris, 5, as.character) x = modify_at(iris, "Sepal.Length", sqrt) str(x)
x = modify_if(iris, is.factor, as.character) str(x) x = modify_at(iris, 5, as.character) x = modify_at(iris, "Sepal.Length", sqrt) str(x)
Create a Named List
named_list(nn = character(0L), init = NULL)
named_list(nn = character(0L), init = NULL)
nn |
( |
init |
( |
(named list()
).
named_list(c("a", "b")) named_list(c("a", "b"), init = 1)
named_list(c("a", "b")) named_list(c("a", "b"), init = 1)
Creates a simple atomic vector with init
as values.
named_vector(nn = character(0L), init = NA)
named_vector(nn = character(0L), init = NA)
nn |
( |
init |
( |
(named vector()
).
named_vector(c("a", "b"), NA) named_vector(character())
named_vector(c("a", "b"), NA) named_vector(character())
A simple wrapper around base::names()
.
Returns a character vector even if no names attribute is set.
Values NA
and ""
are treated as missing and replaced with the value provided in missing_val
.
names2(x, missing_val = NA_character_)
names2(x, missing_val = NA_character_)
x |
( |
missing_val |
( |
(character(length(x))
).
x = 1:3 names(x) names2(x) names(x)[1:2] = letters[1:2] names(x) names2(x, missing_val = "")
x = 1:3 names(x) names2(x) names(x)[1:2] = letters[1:2] names(x) names2(x, missing_val = "")
Simply opens a manual page specified in "package::topic" syntax.
open_help(man)
open_help(man)
man |
( |
Nothing.
catf()
, messagef()
, warningf()
and stopf()
are wrappers around base::cat()
,
base::message()
, base::warning()
and base::stop()
, respectively.
The call is not included for warnings and errors.
catf(msg, ..., file = "", wrap = FALSE) messagef(msg, ..., wrap = FALSE) warningf(msg, ..., wrap = FALSE) stopf(msg, ..., wrap = FALSE)
catf(msg, ..., file = "", wrap = FALSE) messagef(msg, ..., wrap = FALSE) warningf(msg, ..., wrap = FALSE) stopf(msg, ..., wrap = FALSE)
msg |
( |
... |
( |
file |
( |
wrap |
( |
messagef(" This is a rather long %s on multiple lines which will get wrapped. ", "string", wrap = 15)
messagef(" This is a rather long %s on multiple lines which will get wrapped. ", "string", wrap = 15)
Performs base::cbind()
on data.tables, possibly by reference.
rcbind(x, y)
rcbind(x, y)
x |
( |
y |
( |
(data.table::data.table()
): Updated x
.
x = data.table::data.table(a = 1:3, b = 3:1) y = data.table::data.table(c = runif(3)) rcbind(x, y)
x = data.table::data.table(a = 1:3, b = 3:1) y = data.table::data.table(c = runif(3)) rcbind(x, y)
rd_info()
is an internal generic to generate Rd or markdown code to be used in manual pages.
rd_format_string()
and rd_format_range()
are string functions to assist generating
proper Rd code.
rd_info(obj, ...) rd_format_range(lower, upper) rd_format_string(str, quote = c("\\dQuote{", "}")) rd_format_packages(packages)
rd_info(obj, ...) rd_format_range(lower, upper) rd_format_string(str, quote = c("\\dQuote{", "}")) rd_format_packages(packages)
obj |
( |
... |
( |
lower |
( |
upper |
( |
str |
( |
quote |
( Will be replicated to lenght 2. |
packages |
( |
character()
, possibly with markdown code.
Repeats all vectors of a list .x
to the length of the longest vector
using rep()
with argument length.out
.
This operation will only work
if the length of the longest vectors is an integer multiple of all shorter
vectors, and will throw an exception otherwise.
recycle_vectors(.x)
recycle_vectors(.x)
.x |
( |
(list()
) with vectors of same size.
recycle_vectors(list(a = 1:3, b = 2))
recycle_vectors(list(a = 1:3, b = 2))
Register a function callback
to be called after a namespace is loaded.
Calls callback
once if the namespace has already been loaded before and
also adds an unload-hook that removes the load hook.
register_namespace_callback(pkgname, namespace, callback)
register_namespace_callback(pkgname, namespace, callback)
pkgname |
( |
namespace |
( |
callback |
( |
NULL
.
Returns an integer vector to order vector x
according to vector y
.
reorder_vector(x, y, na_last = NA)
reorder_vector(x, y, na_last = NA)
x |
( |
y |
( |
na_last |
(
|
(integer()
).
# x subset of y x = c("b", "a", "c", "d") y = letters x[reorder_vector(x, y)] # y subset of x y = letters[1:3] x[reorder_vector(x, y)] x[reorder_vector(x, y, na_last = TRUE)] x[reorder_vector(x, y, na_last = FALSE)]
# x subset of y x = c("b", "a", "c", "d") y = letters x[reorder_vector(x, y)] # y subset of x y = letters[1:3] x[reorder_vector(x, y)] x[reorder_vector(x, y, na_last = TRUE)] x[reorder_vector(x, y, na_last = FALSE)]
Packages are loaded (not attached) via base::requireNamespace()
.
If at least on package can not be loaded, an exception of class "packageNotFoundError" is raised.
The character vector of missing packages is stored in the condition as packages
.
require_namespaces( pkgs, msg = "The following packages could not be loaded: %s", quietly = FALSE )
require_namespaces( pkgs, msg = "The following packages could not be loaded: %s", quietly = FALSE )
pkgs |
( |
msg |
( |
quietly |
( |
(character()
) of loaded packages (invisibly).
require_namespaces("mlr3misc") # catch condition, return missing packages tryCatch(require_namespaces(c("mlr3misc", "foobaaar")), packageNotFoundError = function(e) e$packages)
require_namespaces("mlr3misc") # catch condition, return missing packages tryCatch(require_namespaces(c("mlr3misc", "foobaaar")), packageNotFoundError = function(e) e$packages)
Similar to the tibble function tribble()
, this function
allows to construct tabular data in a row-wise fashion.
The first arguments passed as formula will be interpreted as column names. The remaining arguments will be put into the resulting table.
rowwise_table(..., .key = NULL)
rowwise_table(..., .key = NULL)
... |
( |
.key |
( |
rowwise_table( ~a, ~b, 1, "a", 2, "b" )
rowwise_table( ~a, ~b, 1, "a", 2, "b" )
seq_row()
creates a sequence along the number of rows of x
,
seq_col()
a sequence along the number of columns of x
.
seq_len0()
and seq_along0()
are the 0-based counterparts to base::seq_len()
and
base::seq_along()
.
seq_row(x) seq_col(x) seq_len0(n) seq_along0(x)
seq_row(x) seq_col(x) seq_len0(n) seq_along0(x)
x |
( |
n |
( |
seq_len0(3)
seq_len0(3)
Simple wrapper for class(x) = classes
.
set_class(x, classes)
set_class(x, classes)
x |
( |
classes |
( |
Object x
, with updated class attribute.
set_class(list(), c("foo1", "foo2"))
set_class(list(), c("foo1", "foo2"))
Sets the names (or colnames) of x
to nm
.
If nm
is a function, it is used to transform the already existing names of x
.
set_names(x, nm = x, ...) set_col_names(x, nm, ...)
set_names(x, nm = x, ...) set_col_names(x, nm, ...)
x |
( |
nm |
( |
... |
( |
x
with updated names.
x = letters[1:3] # name x with itself: x = set_names(x) print(x) # convert names to uppercase x = set_names(x, toupper) print(x)
x = letters[1:3] # name x with itself: x = set_names(x) print(x) # convert names to uppercase x = set_names(x, toupper) print(x)
Convenience function to modfiy (or overwrite) the values of a paradox::ParamSet.
set_params(.ps, ..., .values = list(), .insert = TRUE)
set_params(.ps, ..., .values = list(), .insert = TRUE)
.ps |
(paradox::ParamSet) |
... |
(any) Named parameter values. |
.values |
( |
.insert |
( |
if (requireNamespace("paradox")) { param_set = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl()) param_set$values$a = 0 set_params(param_set, a = 1, .values = list(b = 2), .insert = TRUE) set_params(param_set, a = 3, .insert = FALSE) set_params(param_set, b = 4, .insert = TRUE) }
if (requireNamespace("paradox")) { param_set = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl()) param_set$values$a = 0 set_params(param_set, a = 1, .values = list(b = 2), .insert = TRUE) set_params(param_set, a = 3, .insert = FALSE) set_params(param_set, b = 4, .insert = TRUE) }
A version of sample()
which does not treat positive scalar integer x
differently.
See example.
shuffle(x, n = length(x), ...)
shuffle(x, n = length(x), ...)
x |
( |
n |
( |
... |
( |
x = 2:3 sample(x) shuffle(x) x = 3 sample(x) shuffle(x)
x = 2:3 sample(x) shuffle(x) x = 3 sample(x) shuffle(x)
Collapse multiple strings into a single string.
str_collapse(str, sep = ", ", quote = character(), n = Inf, ellipsis = "[...]")
str_collapse(str, sep = ", ", quote = character(), n = Inf, ellipsis = "[...]")
str |
( |
sep |
( |
quote |
( Will be replicated to lenght 2. |
n |
( |
ellipsis |
( |
(character(1)
).
str_collapse(letters, quote = "'", n = 5)
str_collapse(letters, quote = "'", n = 5)
Formats a text block for printing.
str_indent(initial, str, width = 0.9 * getOption("width"), exdent = 2L, ...)
str_indent(initial, str, width = 0.9 * getOption("width"), exdent = 2L, ...)
initial |
( |
str |
( |
width |
( |
exdent |
( |
... |
( |
(character()
).
cat(str_indent("Letters:", str_collapse(letters), width = 25), sep = "\n")
cat(str_indent("Letters:", str_collapse(letters), width = 25), sep = "\n")
str_trunc()
truncates a string to a given width.
str_trunc(str, width = 0.9 * getOption("width"), ellipsis = "[...]")
str_trunc(str, width = 0.9 * getOption("width"), ellipsis = "[...]")
str |
( |
width |
( |
ellipsis |
( |
(character()
).
str_trunc("This is a quite long string", 20)
str_trunc("This is a quite long string", 20)
Converts a logical vector from binary to decimal.
The bit vector may have any length, the last position is the least significant, i.e.
bits are multiplied with 2^(n-1)
, 2^(n-2)
, ..., 2^1
, 2^0
where n
is the
length of the bit vector.
to_decimal(bits)
to_decimal(bits)
bits |
( |
(integer(1)
).
Topologically sort a graph, where we are passed node labels and a list of direct
parents for each node, as labels, too.
A node can be 'processed' if all its parents have been 'processed',
and hence occur at previous indices in the resulting sorting.
Returns a table, in topological row order for IDs, and an entry depth
,
which encodes the topological layer, starting at 0.
So nodes with depth == 0
are the ones with no dependencies,
and the one with maximal depth
are the ones on which nothing else depends on.
topo_sort(nodes)
topo_sort(nodes)
nodes |
(
|
(data.table::data.table()
) with columns id
, depth
, sorted topologically for IDs.
nodes = rowwise_table( ~id, ~parents, "a", "b", "b", "c", "c", character() ) topo_sort(nodes)
nodes = rowwise_table( ~id, ~parents, "a", "b", "b", "c", "c", character() ) topo_sort(nodes)
Transposes a list of list, and turns it inside out, similar to the
function transpose()
in package purrr.
transpose_list(.l)
transpose_list(.l)
.l |
( |
list()
.
x = list(list(a = 2, b = 3), list(a = 5, b = 10)) str(x) str(transpose_list(x)) # list of data frame rows: transpose_list(iris[1:2, ])
x = list(list(a = 2, b = 3), list(a = 5, b = 10)) str(x) str(transpose_list(x)) # list of data frame rows: transpose_list(iris[1:2, ])
Transforms list columns to separate columns, possibly by reference. The original columns are removed from the returned table. All non-atomic objects in the list columns are expand to new list column.
unnest(x, cols, prefix = NULL)
unnest(x, cols, prefix = NULL)
x |
( |
cols |
( |
prefix |
( |
x = data.table::data.table( id = 1:2, value = list(list(a = 1, b = 2), list(a = 2, b = 2)) ) print(x) unnest(data.table::copy(x), "value") unnest(data.table::copy(x), "value", prefix = "{col}.")
x = data.table::data.table( id = 1:2, value = list(list(a = 1, b = 2), list(a = 2, b = 2)) ) print(x) unnest(data.table::copy(x), "value") unnest(data.table::copy(x), "value", prefix = "{col}.")
Works similar to base::which.min()
/base::which.max()
, but corrects for ties.
Missing values are treated as Inf
for which_min
and as -Inf
for which_max()
.
which_min(x, ties_method = "random", na_rm = FALSE) which_max(x, ties_method = "random", na_rm = FALSE)
which_min(x, ties_method = "random", na_rm = FALSE) which_max(x, ties_method = "random", na_rm = FALSE)
x |
( |
ties_method |
( |
na_rm |
( |
(integer()
): Index of the minimum/maximum value.
Returns an empty integer vector for empty input vectors and vectors with no non-missing values
(if na_rm
is TRUE
).
Returns NA
if na_rm
is FALSE
and at least one NA
is found in x
.
x = c(2, 3, 1, 3, 5, 1, 1) which_min(x, ties_method = "first") which_min(x, ties_method = "last") which_min(x, ties_method = "random") which_max(x) which_max(integer(0)) which_max(NA) which_max(c(NA, 1))
x = c(2, 3, 1, 3, 5, 1, 1) which_min(x, ties_method = "first") which_min(x, ties_method = "last") which_min(x, ties_method = "random") which_max(x) which_max(integer(0)) which_max(NA) which_max(c(NA, 1))
Attaches a package to the search path (if not already attached), executes code and eventually removes the package from the search path again, restoring the previous state.
Note that this function is deprecated in favor of the (now fixed) version in withr.
with_package(package, code, ...)
with_package(package, code, ...)
package |
( |
code |
( |
... |
( |
Result of the evaluation of code
.
withr package.