1- # sfmisc utils 0.0.1.9003
1+ # sfmisc utils 0.0.1.9010
22
33
44
@@ -25,15 +25,64 @@ walk <- function(.x, .f, ...){
2525
2626# assertions --------------------------------------------------------------
2727
28+ # ' Assert a condition
29+ # '
30+ # ' A simpler and more efficient for [base::stopifnot()] that has an easy
31+ # ' mechanism for supplying custom error messages. As opposed to `stopifnot()`,
32+ # ' `assert()` only works with a single (scalar) assertions.
33+ # '
34+ # ' @param cond `TRUE` or `FALSE` (without any attributes). `FALSE` will throw
35+ # ' an exception with an automatically constructed error message (if `...`
36+ # ' was not supplied). Anything else will throw an exception stating that
37+ # ' `cond` was not valid.
38+ # ' @param ... passed on to [stop()]
39+ # ' @param call. passed on to [stop()]
40+ # ' @param domain passed on to [stop()]
41+ # '
42+ # ' @noRd
43+ # '
44+ # ' @return TRUE on success
45+ # '
46+ # ' @examples
47+ # '
48+ # ' \dontrun{
49+ # ' assert(1 == 1)
50+ # ' assert(1 == 2)
51+ # ' }
52+ # '
53+ # '
54+ assert <- function (
55+ cond ,
56+ ... ,
57+ call. = FALSE ,
58+ domain = NULL
59+ ){
60+ if (identical(cond , TRUE )){
61+ return (TRUE )
62+ } else if (identical(cond , FALSE )){
63+ if (identical(length(list (... )), 0L )){
64+ msg <- paste0(" `" , deparse(match.call()[[2 ]]), " `" , " is not 'TRUE'" )
65+ stop(msg , call. = call. , domain = domain )
66+ } else {
67+ suppressWarnings( stop(... , call. = call. , domain = domain ) )
68+ }
69+
70+ } else {
71+ stop(" Assertion must be either 'TRUE' or 'FALSE'" )
72+ }
73+ }
74+
75+
76+
77+
2878assert_namespace <- function (x ){
29- stopifnot (requireNamespace(x , quietly = TRUE ))
79+ assert (requireNamespace(x , quietly = TRUE ))
3080 invisible (TRUE )
3181}
3282
3383
3484
3585
36-
3786# conditions --------------------------------------------------------------
3887
3988# ' Condition constructor
@@ -88,15 +137,23 @@ condition <- function(subclass, message, call = sys.call(-1), ...) {
88137
89138
90139
91- # ' @rdname condition
92140error <- function (subclass , message , call = sys.call(- 1 ), ... ) {
93141 structure(
94142 class = c(subclass , " error" , " condition" ),
95143 list (message = message , call = call , ... )
96144 )
97145}
98146
147+
148+
149+
99150# predicates --------------------------------------------------------------
151+ is_scalar <- function (x ){
152+ identical(length(x ), 1L )
153+ }
154+
155+
156+
100157
101158is_scalar_character <- function (x ){
102159 is.character(x ) && is_scalar(x )
@@ -119,18 +176,25 @@ is_scalar_integerish <- function(x){
119176
120177
121178
122- is_scalar <- function (x ){
123- identical(length(x ), 1L )
179+ is_tf <- function (x ){
180+ is.logical(x ) && ! anyNA(x )
181+ }
182+
183+
184+
185+
186+ is_scalar_tf <- function (x ){
187+ identical(x , TRUE ) || identical(x , FALSE )
124188}
125189
126190
127191
128192
129193is_integerish <- function (x ){
130194 if (! is.numeric(x )){
131- vector( " logical " , length( x ))
195+ FALSE
132196 } else {
133- as.integer(x ) == x
197+ all( as.integer(x ) == x )
134198 }
135199}
136200
@@ -158,6 +222,7 @@ is_blank <- function(x){
158222
159223
160224
225+
161226# all_are -----------------------------------------------------------------
162227
163228
@@ -178,7 +243,7 @@ is_blank <- function(x){
178243# ' all_are_identical(c(1,1,1))
179244# '
180245all_are_identical <- function (x , empty_value = FALSE ) {
181- assert_that (length(empty_value ) < = 1 )
246+ assert (length(empty_value ) < = 1 )
182247
183248 if (length(x ) > 0L ) {
184249 return (identical(length(unique(x )), 1L ))
@@ -216,7 +281,7 @@ all_are_distinct <- function(
216281 x ,
217282 empty_value = FALSE
218283){
219- assert_that (length(empty_value ) < = 1 )
284+ assert (length(empty_value ) < = 1 )
220285
221286 if (identical(length(x ), 1L )) {
222287 return (TRUE )
@@ -235,4 +300,3 @@ all_are_distinct <- function(
235300 return (empty_value )
236301 }
237302}
238-
0 commit comments