forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-replace-with.R
55 lines (43 loc) · 1.03 KB
/
utils-replace-with.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
replace_with <- function(x, i, val, name) {
if (is.null(val)) {
return(x)
}
check_length(val, x, name)
check_type(val, x, name)
check_class(val, x, name)
if (length(val) == 1L) {
x[i] <- val
} else {
x[i] <- val[i]
}
x
}
check_length <- function(x, template, name = deparse(substitute(x))) {
n <- length(template)
if (length(x) == n) {
return()
}
if (length(x) == 1L) {
return()
}
stop(name, " is length ", length(x), " not 1 or ", n, ".", call. = FALSE)
}
check_type <- function(x, template, name = deparse(substitute(x))) {
if (identical(typeof(x), typeof(template))) {
return()
}
stop(
name, " has type '", typeof(x), "' not '", typeof(template), "'",
call. = FALSE
)
}
check_class <- function(x, template, name = deparse(substitute(x))) {
if (!is.object(x)) {
return()
}
if (identical(class(x), class(template))) {
return()
}
stop(name, " has class ", paste(class(x), collapse = "/"), " not ",
paste(class(template), collapse = "/"), call. = FALSE)
}