Type: | Package |
Title: | Shorthand if-Else |
Version: | 0.1.0 |
Maintainer: | Dawid Kałędkowski <dawid.kaledkowski@gmail.com> |
Description: | Short hand if-else function to easily switch the values depending on a logical condition. |
License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
BugReports: | https://github.com/gogonzo/ox/issues |
Encoding: | UTF-8 |
Language: | en-US |
Depends: | R (≥ 3.0) |
Suggests: | checkmate, dplyr, magrittr, knitr, rmarkdown, testthat, utils |
RoxygenNote: | 7.1.2 |
VignetteBuilder: | knitr |
NeedsCompilation: | no |
Packaged: | 2021-12-14 07:10:41 UTC; gonzo |
Author: | Dawid Kałędkowski |
Repository: | CRAN |
Date/Publication: | 2021-12-15 09:00:02 UTC |
Utility function to run OX
Description
Utility function to run OX
Usage
OX_default(
.f,
...,
.then = list(...)[[1]],
.else = rev(list(...))[[1]],
.invert = FALSE
)
Arguments
.f |
( |
... |
arguments passed to the |
.then |
( |
.else |
( |
.invert |
( |
Checks .then and .else for OX
Description
Checks if .then and .else are fine. If they have supported classes and if their length is the same.
Usage
check_thenelse_OX(.then, .else)
Arguments
.then |
( |
.else |
( |
Value
NULL
invisible.
Indices inversion
Description
Inverts the indices to select the rest. For example when idx
is logical
then [!idx]
, if integer
then [-idx]
Usage
invert_indices(idx)
Arguments
idx |
( |
Value
object identical to idx
input but with inverted values.
Short hand if-else
Description
Short hand if-else functions for simple values switching.
Usage
ox(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
xo(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
Arguments
.f |
( |
... |
arguments passed to the |
.then |
A positive-replacement. NOTE, that if |
.else |
A negative-replacement. NOTE, that if |
Details
ox
evaluates function .f
which returns a single logical value and
depending on the result ox
returns:
on
TRUE
returns.then
.on
FALSE
returns.else
.
It's important to note is that .then
and .else
have a default values set
as the first and the last element of the ...
. This means that they don't
have to be specified if both are arguments of .f
.
If any of them are not arguments of .f
then this argument should be
specified directly as .then
or .else
, otherwise it will be passed to the
function.
As, far as .then
and .else
won't be specified order of arguments in ...
matters for ox
but it's up to you if they are passed in the correct order
to the .f
. Then one, might consider name the argument so .f
will be
executed as expected. Consider following example
greater_than_by <- function(x, y, by) x > (y + by) ox(greater_than_by, x = 5, by = 3, y = 3)
In above, one needs to move y
to the end so that it will be considered as
.else
.
To invert the switch one can use xo
which is equivalent of
ox(Negate(.f), ..., .then, .else)
.
Value
object identical to .then
or .else
depending on the condition
result.
Examples
# if (is.null(NULL)) NULL else 1
ox(NULL, .f = is.null, .else = 1)
# if (is("text", "character")) "text" else "not a character"
ox("text", .f = is, "character", .else = "not a character")
# if (1 > 2) 1 else 2
ox(`>`, 1, 2)
# if (!is.null(NULL)) NULL else 1
xo(NULL, .f = is.null, .else = 1)
# if (!is("text", "character")) "text" else "not a character"
xo("text", .f = is, "character", .else = "not a character")
# if (!1 > 2) 1 else 2
xo(`>`, 1, 2)
Check .f
output
Description
Checks whether .f
has needed length
Usage
validate_f_out(idx, len)
Arguments
idx |
( |
len |
( |
Value
idx
if logical
and unique(idx)
if numeric.
Vectorized ox
Description
Switch the values of the vector by another on specific indices.
Usage
OX(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
XO(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
Arguments
.f |
( |
... |
arguments passed to the |
.then |
( |
.else |
( |
Details
#' OX
evaluates function .f
which returns a vector of indices which
are then decide which values of .then
are replaced by else
.
.then[!idx] <- .else[!idx]
Consequence of above is that idx = .f(...)
should be a logical
vector or
integer
vector which would be valid indices for .then
and .else
.
This means that .then
and .else
should be of the same length, but there
are two exceptions:
when
.else
is a single value, than this value will replace.then
at returned indices.then[!idx] <- .else
when
.else
isNULL
To invert the switch one can use XO
which is equivalent of
OX(Negate(.f), ..., .then, .else)
.
Value
atomic
or list
. Returned object is a .then
object with elements
replaced by .else
depending on a result of the logical condition.
Examples
# switch values of the vector when condition is true
OX(is.na, c(1, NA, 3), .else = c(2, 2, 2))
# use OX to invert negate the condition
XO(is.na, c(1, NA, 3), .else = c(2, 2, 2))