library(tinycodet)
#> Run `?tinycodet::tinycodet` to open the introduction help page of 'tinycodet'.
source_selection
The source_selection()
function is the same as base R’s
source()
function, except that it allows only placing the
selected objects and functions into the current environment, instead of
all objects.
The objects to be selected can be specified using any combination of the following:
- by supplying a character vector of exact object names to the
select
argument. - by supplying a character vector of
regex
patterns to theregex
argument. - by supplying a character vector of
fixed
patterns to thefixed
argument.
The regex
and fixed
arguments are
especially handy when sourcing S3 methods. For example, to expose the
following methods to the current environment,
mymethod.numeric()
and mymethod.character()
from generic mymethod()
, one could specify
regex = "^mymethod"
.
Example:
exprs <- expression({
helloworld = function()print("helloworld")
goodbyeworld <- function() print("goodbye world")
`%s+test%` <- function(x,y) stringi::`%s+%`(x,y)
`%s*test%` <- function(x,y) stringi::`%s*%`(x,y)
mymethod <- function(x) UseMethod("mymethod", x)
mymethod.numeric <- function(x)x * 2
mymethod.character <- function(x)chartr(x, old = "a-zA-Z", new = "A-Za-z")
})
temp.fun <- function(){
source_selection(list(exprs=exprs), regex= "^mymethod", fixed = c("%", ":="))
ls() # list all objects residing within the function definition
}
temp.fun()
#> Sourcing script ...
#> Done
#> [1] "%s*test%" "%s+test%" "mymethod"
#> [4] "mymethod.character" "mymethod.numeric"
temp.fun <- function(){
source_selection(list(exprs=exprs), select = c("helloworld", "goodbyeworld"))
ls() # list all objects residing within the function definition
}
temp.fun()
#> Sourcing script ...
#>
#> Done
#> [1] "goodbyeworld" "helloworld"
Additional logic operators
The tinycodet package adds a few basic logic operators:
-
%xor%
: Exclusive OR -
%n&%
: NOT AND (i.e.(!x) & (!y)
). Note that if eitherx
ory
isNA
,%n&%
will also giveNA
(unlike(!x) & (!y)
, which would giveFALSE
.) -
%?=%
: checks if bothx
andy
are unknown or unreal (NA, NaN, Inf, -Inf) -
%out%
: the opposite of%in%
(i.e.!x %in% y
)
Here are some examples:
x <- c(TRUE, FALSE, TRUE, FALSE, NA, NaN, Inf, -Inf, TRUE, FALSE)
y <- c(FALSE, TRUE, TRUE, FALSE, rep(NA, 6))
outcome <- data.frame(
x=x, y=y,
"x %xor% y"=x %xor% y, "x %n&% y" = x %n&% y, "x %?=% y" = x %?=% y,
check.names = FALSE
)
knitr::kable(outcome)
x | y | x %xor% y | x %n&% y | x %?=% y |
---|---|---|---|---|
1 | FALSE | TRUE | FALSE | FALSE |
0 | TRUE | TRUE | FALSE | FALSE |
1 | TRUE | FALSE | FALSE | FALSE |
0 | FALSE | FALSE | TRUE | FALSE |
NA | NA | NA | NA | TRUE |
NaN | NA | NA | NA | TRUE |
Inf | NA | NA | FALSE | TRUE |
-Inf | NA | NA | FALSE | TRUE |
1 | NA | NA | FALSE | FALSE |
0 | NA | NA | NA | FALSE |
1:3 %out% 1:10
#> [1] FALSE FALSE FALSE
1:10 %out% 1:3
#> [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Numbers can have many different sub-types whilst still being
numeric
. The n %=numtype% numtype
operator
will check for every value of numeric vector n
if it can be
considered a number belonging to type numtype
. The
following values for numtype
are allowed:
- “~0”: zero, or else a number whose absolute value is smaller than
the Machine tolerance (
sqrt(.Machine$double.eps)
); - “B”: binary numbers (0 or 1);
- “prop”: proportions;
- “I”: Integers;
- “odd”: odd integers;
- “even”: even integers
- “R”: Real numbers;
- “unreal”: infinity, NA, or NaN;
The string counterpart for %=numtype%
is
s %=strtype% strtype
, which checks for every value of
character vector s
if it can seen as a certain
strtype
. The following values for strtype
are
allowed:
- “empty”: checks if the string only consists of empty spaces.
- “unreal”: checks if the string is NA, or if it has literal string “NA”, “NaN” or “Inf”, regardless if it has leading or trailing spaces.
- “numeric”: checks if the string can be converted to a number, disregarding leading and trailing spaces. I.e. the string “5.0” can be converted to the the actual number 5.0.
- “special”: checks if the string consists of only special characters.
Here are some examples:
1e-20 %=numtype% "~0"
#> [1] TRUE
n <- c(0:5, 0:-5, 0.1, -0.1, 0, 1, Inf, -Inf, NA, NaN)
n[n %=numtype% "B"]
#> [1] 0 1 0 0 1
n[n %=numtype% "prop"]
#> [1] 0.0 1.0 0.0 0.1 0.0 1.0
n[n %=numtype% "B"]
#> [1] 0 1 0 0 1
n[n %=numtype% "I"]
#> [1] 0 1 2 3 4 5 0 -1 -2 -3 -4 -5 0 1
n[n %=numtype% "odd"]
#> [1] 1 3 5 -1 -3 -5 1
n[n %=numtype% "even"]
#> [1] 0 2 4 0 -2 -4 0
n[n %=numtype% "R"]
#> [1] 0.0 1.0 2.0 3.0 4.0 5.0 0.0 -1.0 -2.0 -3.0 -4.0 -5.0 0.1 -0.1 0.0
#> [16] 1.0
n[n %=numtype% "unreal"]
#> [1] Inf -Inf NA NaN
s <- c(" AbcZ123 ", " abc ", " 1.3 ", " !#$%^&*() ", " ", " NA ", " NaN ", " Inf ")
s[s %=strtype% "empty"]
#> [1] " "
s[s %=strtype% "unreal"]
#> [1] " NA " " NaN " " Inf "
s[s %=strtype% "numeric"]
#> [1] " 1.3 " " Inf "
s[s %=strtype% "special"]
#> [1] " !#$%^&*() "