Title: | Basic Infix Binary Operators |
---|---|
Description: | Contains a number of infix binary operators that may be useful in day to day practices. |
Authors: | Ernest Benedito [aut, cre] |
Maintainer: | Ernest Benedito <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2024-10-21 03:01:21 UTC |
Source: | https://github.com/ebeneditos/infix |
Use this method to handle errors. The function evaluates an expression, and if it raises an error then evaluates a second expression.
tryExcept(expr, except = { }, error = function(e) { }) expr %except% except
tryExcept(expr, except = { }, error = function(e) { }) expr %except% except
expr |
Expression to be evaluated. |
except |
Expression to be evaluated if |
error |
Handler function for an |
tryExcept
is a wrapper around tryCatch
,
but it allows you to evaluate an expression except
when an error occurs to
the first expression argument expr
. Note that, if expr
raises
an error, the code evaluated before the error will be in use.
# No errors are raised tryExcept(stop()) # If 'expr' has no errors tryExcept({ foo <- "foo" }, except = { foo <- "foo bar" }) print(foo) # "foo" # If 'expr' has an error tryExcept({ foo <- "foo" stop() }, except = { foo <- "foo bar" }) print(foo) # "foo bar" # Running it with the infix operator {foo <- "foo"} %except% {foo <- "foo bar"} print(foo) # "foo" { foo <- "foo" stop() } %except% { foo <- "foo bar" } print(foo) # "foo bar"
# No errors are raised tryExcept(stop()) # If 'expr' has no errors tryExcept({ foo <- "foo" }, except = { foo <- "foo bar" }) print(foo) # "foo" # If 'expr' has an error tryExcept({ foo <- "foo" stop() }, except = { foo <- "foo bar" }) print(foo) # "foo bar" # Running it with the infix operator {foo <- "foo"} %except% {foo <- "foo bar"} print(foo) # "foo" { foo <- "foo" stop() } %except% { foo <- "foo bar" } print(foo) # "foo bar"
Analogous to file.path(x, y)
.
x %//% y
x %//% y
x , y
|
Character vectors. |
"home" %//% "dir" # returns "home/dir"
"home" %//% "dir" # returns "home/dir"
Creates a function which returns the corresponding logical
operation between what f1
and f2
return.
f1 %&% f2 f1 %|% f2 f1 %xor% f2
f1 %&% f2 f1 %|% f2 f1 %xor% f2
f1 , f2
|
Arbitrary predicate functions. |
To obtain the logical negation of what a function f
returns,
use base
function Negate
.
is.null.na <- is.null %|% is.na all(is.null.na(NA), is.null.na(NULL)) # returns TRUE
is.null.na <- is.null %|% is.na all(is.null.na(NA), is.null.na(NULL)) # returns TRUE
Contains a number of infix binary operators that may be useful in day to day practices.
except
Simple Error Handling.
operators
Common Infix Operators.
funlogic
Function Logical Operators.
pipes
Package's 'magrittr' pipe-operators.
Maintainer: Ernest Benedito [email protected]
Useful links:
NULL
This infix function makes it easy to replace NULL
's with
a default value. It's inspired by the way that Ruby's or operation
(`||`
) works.
a %||% b
a %||% b
a , b
|
If |
1 %||% 2 # returns 1 NULL %||% 2 # returns 2
1 %||% 2 # returns 1 NULL %||% 2 # returns 2
The logical negation of %in%
.
x %!in% table
x %!in% table
x |
Vector or |
table |
Vector or |
4 %!in% 1:3 # returns TRUE
4 %!in% 1:3 # returns TRUE
Common Infix Operators
paste0
Concatenate Strings (see ?"%+%"
).
file.path
Construct Path to File (see ?"%//%"
).
nomatch
Value (non) Matching (see ?"%!in%"
).
nil
Default value for NULL
(see ?"%||%"
).
Analogous to paste0(x, y)
.
x %+% y
x %+% y
x , y
|
Objects to be converted to character vectors. |
"01" %+% "jan" %+% "1970" # returns "01jan1970"
"01" %+% "jan" %+% "1970" # returns "01jan1970"