1-
2-
31backticks <- function (x ){
42 paste0(" `" , x , " `" )
53}
@@ -41,34 +39,6 @@ fmt_calls <- function(...){
4139
4240
4341
44- check_length_val <- function (
45- length_x ,
46- n ,
47- header ,
48- reason = NULL ,
49- .stop = stop
50- ){
51- if (all(length_x %in% c(1L , n )))
52- return ()
53-
54- if (is.null(reason ))
55- reason <- " "
56- else
57- reason <- paste0(" (" , reason , " )" )
58-
59- if (is.null(header ))
60- header <- " "
61- else
62- header <- paste0(header , " " )
63-
64-
65- if (n == 1 ) {
66- .stop(sprintf(" %smust be length 1%s, not %s" , header , reason , paste(length_x , collapse = " , " )))
67- } else {
68- .stop(sprintf(" %smust be length %s%s or one, not %s" , header , n , reason , paste(length_x , collapse = " , " )))
69- }
70- }
71-
7242
7343
7444
@@ -83,9 +53,9 @@ replace_with <- function (
8353 return (x )
8454 }
8555
86- check_length (val , x , name , reason )
87- check_type (val , x , name )
88- check_class (val , x , name )
56+ assert_length_1_or_n (val , length( x ) , name , reason )
57+ assert_equal_type (val , x , name )
58+ assert_equal_class (val , x , name )
8959
9060 i [is.na(i )] <- FALSE
9161 if (length(val ) == 1L ) {
@@ -99,26 +69,13 @@ replace_with <- function (
9969
10070
10171
102- check_length <- function (
103- x ,
104- template ,
105- header ,
106- reason = NULL
107- ){
108- check_length_val(length(x ), length(template ), header , reason )
109- }
110-
111-
112-
113-
114- check_type <- function (
72+ assert_equal_type <- function (
11573 x ,
11674 template ,
11775 header
11876){
119- if (identical(typeof(x ), typeof(template ))) {
120- return ()
121- }
77+ if (identical(typeof(x ), typeof(template )))
78+ return (TRUE )
12279
12380 if (is.null(header ))
12481 header <- " "
@@ -131,16 +88,16 @@ check_type <- function(
13188
13289
13390
134- check_class <- function (
91+ assert_equal_class <- function (
13592 x ,
13693 template ,
13794 header
13895){
13996 if (! is.object(x )) {
140- return ()
97+ return (TRUE )
14198
14299 } else if (identical(class(x ), class(template ))) {
143- return ()
100+ return (TRUE )
144101
145102 } else {
146103
@@ -150,15 +107,77 @@ check_class <- function(
150107 header <- paste0(header , " " )
151108
152109
153- stop(sprintf(" %smust be type %s, not %s" , header , typeof(template ), typeof(x )))
110+ stop(
111+ sprintf(
112+ " %smust be %s, not %s" ,
113+ header ,
114+ paste(class(template ), collapse = " /" ),
115+ paste(class(x ), collapse = " /" )
116+ )
117+ )
154118 }
155119}
156120
157121
158122
159123
160- fmt_classes <- function (
161- x
124+ check_length_1_or_n <- function (
125+ x ,
126+ n ,
127+ header ,
128+ reason
162129){
163- paste(class(x ), collapse = " /" )
130+ if (length(x ) %in% c(1 , n )){
131+ return (NULL )
132+ }
133+
134+
135+ if (is.null(reason ))
136+ reason <- " "
137+ else
138+ reason <- paste0(" (" , reason , " )" )
139+
140+ if (is.null(header ))
141+ header <- " "
142+ else
143+ header <- paste0(header , " " )
144+
145+
146+ inconsistent_lengths_message(length(x ), n , header = header , reason = reason )
147+ }
148+
149+
150+
151+
152+ assert_length_1_or_n <- function (
153+ x ,
154+ n ,
155+ header ,
156+ reason
157+ ){
158+ chk <- check_length_1_or_n(x , n , header , reason )
159+
160+ if (is.null(chk )){
161+ TRUE
162+ } else {
163+ stop(chk )
164+ }
165+ }
166+
167+
168+
169+
170+ # messages ----------------------------------------------------------------
171+
172+ inconsistent_lengths_message <- function (
173+ length_is ,
174+ length_should ,
175+ header = " " ,
176+ reason = " "
177+ ){
178+ if (length_should == 1 ) {
179+ sprintf(" %smust be length 1%s, not %s" , header , reason , paste(length_is , collapse = " , " ))
180+ } else {
181+ sprintf(" %smust be length %s%s or one, not %s" , header , length_should , reason , paste(length_is , collapse = " , " ))
182+ }
164183}
0 commit comments