| 1 |
# nocov start --- r-lib/rlang compat-type |
| 2 |
# |
| 3 |
# Changelog |
| 4 |
# ========= |
| 5 |
# |
| 6 |
# 2022-06-22: |
| 7 |
# - `friendly_type_of()` is now `obj_type_friendly()`. |
| 8 |
# - Added `obj_type_oo()`. |
| 9 |
# |
| 10 |
# 2021-12-20: |
| 11 |
# - Added support for scalar values and empty vectors. |
| 12 |
# - Added `stop_input_type()` |
| 13 |
# |
| 14 |
# 2021-06-30: |
| 15 |
# - Added support for missing arguments. |
| 16 |
# |
| 17 |
# 2021-04-19: |
| 18 |
# - Added support for matrices and arrays (#141). |
| 19 |
# - Added documentation. |
| 20 |
# - Added changelog. |
| 21 |
|
| 22 |
|
| 23 |
#' Return English-friendly type |
| 24 |
#' @param x Any R object. |
| 25 |
#' @param value Whether to describe the value of `x`. |
| 26 |
#' @param length Whether to mention the length of vectors and lists. |
| 27 |
#' @return A string describing the type. Starts with an indefinite |
| 28 |
#' article, e.g. "an integer vector". |
| 29 |
#' @noRd |
| 30 |
obj_type_friendly <- function(x, value = TRUE, length = FALSE) { |
| 31 |
if (is_missing(x)) { |
| 32 |
return("absent") |
| 33 |
} |
| 34 |
|
| 35 |
if (is.object(x)) { |
| 36 |
if (inherits(x, "quosure")) { |
| 37 |
type <- "quosure" |
| 38 |
} else { |
| 39 |
type <- paste(class(x), collapse = "/") |
| 40 |
} |
| 41 |
return(sprintf("a <%s> object", type)) |
| 42 |
} |
| 43 |
|
| 44 |
if (!rlang::is_vector(x)) { |
| 45 |
return(.rlang_as_friendly_type(typeof(x))) |
| 46 |
} |
| 47 |
|
| 48 |
n_dim <- length(dim(x)) |
| 49 |
|
| 50 |
if (value && !n_dim) { |
| 51 |
if (is_na(x)) { |
| 52 |
return(switch( |
| 53 |
typeof(x), |
| 54 |
logical = "`NA`", |
| 55 |
integer = "an integer `NA`", |
| 56 |
double = "a numeric `NA`", |
| 57 |
complex = "a complex `NA`", |
| 58 |
character = "a character `NA`", |
| 59 |
.rlang_stop_unexpected_typeof(x) |
| 60 |
)) |
| 61 |
} |
| 62 |
if (length(x) == 1 && !is_list(x)) { |
| 63 |
return(switch( |
| 64 |
typeof(x), |
| 65 |
logical = if (x) "`TRUE`" else "`FALSE`", |
| 66 |
integer = "an integer", |
| 67 |
double = "a number", |
| 68 |
complex = "a complex number", |
| 69 |
character = if (nzchar(x)) "a string" else "`\"\"`", |
| 70 |
raw = "a raw value", |
| 71 |
.rlang_stop_unexpected_typeof(x) |
| 72 |
)) |
| 73 |
} |
| 74 |
if (length(x) == 0) { |
| 75 |
return(switch( |
| 76 |
typeof(x), |
| 77 |
logical = "an empty logical vector", |
| 78 |
integer = "an empty integer vector", |
| 79 |
double = "an empty numeric vector", |
| 80 |
complex = "an empty complex vector", |
| 81 |
character = "an empty character vector", |
| 82 |
raw = "an empty raw vector", |
| 83 |
list = "an empty list", |
| 84 |
.rlang_stop_unexpected_typeof(x) |
| 85 |
)) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
type <- .rlang_as_friendly_vector_type(typeof(x), n_dim) |
| 90 |
|
| 91 |
if (length && !n_dim) { |
| 92 |
type <- paste0(type, sprintf(" of length %s", length(x))) |
| 93 |
} |
| 94 |
|
| 95 |
type |
| 96 |
} |
| 97 |
|
| 98 |
.rlang_as_friendly_vector_type <- function(type, n_dim) { |
| 99 |
if (type == "list") { |
| 100 |
if (n_dim < 2) { |
| 101 |
return("a list") |
| 102 |
} else if (n_dim == 2) { |
| 103 |
return("a list matrix") |
| 104 |
} else { |
| 105 |
return("a list array") |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
type <- switch( |
| 110 |
type, |
| 111 |
logical = "a logical %s", |
| 112 |
integer = "an integer %s", |
| 113 |
numeric = , |
| 114 |
double = "a double %s", |
| 115 |
complex = "a complex %s", |
| 116 |
character = "a character %s", |
| 117 |
raw = "a raw %s", |
| 118 |
type = paste0("a ", type, " %s") |
| 119 |
) |
| 120 |
|
| 121 |
if (n_dim < 2) { |
| 122 |
kind <- "vector" |
| 123 |
} else if (n_dim == 2) { |
| 124 |
kind <- "matrix" |
| 125 |
} else { |
| 126 |
kind <- "array" |
| 127 |
} |
| 128 |
sprintf(type, kind) |
| 129 |
} |
| 130 |
|
| 131 |
.rlang_as_friendly_type <- function(type) { |
| 132 |
switch( |
| 133 |
type, |
| 134 |
|
| 135 |
list = "a list", |
| 136 |
|
| 137 |
NULL = "NULL", |
| 138 |
environment = "an environment", |
| 139 |
externalptr = "a pointer", |
| 140 |
weakref = "a weak reference", |
| 141 |
S4 = "an S4 object", |
| 142 |
|
| 143 |
name = , |
| 144 |
symbol = "a symbol", |
| 145 |
language = "a call", |
| 146 |
pairlist = "a pairlist node", |
| 147 |
expression = "an expression vector", |
| 148 |
|
| 149 |
char = "an internal string", |
| 150 |
promise = "an internal promise", |
| 151 |
... = "an internal dots object", |
| 152 |
any = "an internal `any` object", |
| 153 |
bytecode = "an internal bytecode object", |
| 154 |
|
| 155 |
primitive = , |
| 156 |
builtin = , |
| 157 |
special = "a primitive function", |
| 158 |
closure = "a function", |
| 159 |
|
| 160 |
type |
| 161 |
) |
| 162 |
} |
| 163 |
|
| 164 |
.rlang_stop_unexpected_typeof <- function(x, call = rlang::caller_env()) { |
| 165 |
rlang::abort( |
| 166 |
sprintf("Unexpected type <%s>.", typeof(x)), |
| 167 |
call = call |
| 168 |
) |
| 169 |
} |
| 170 |
|
| 171 |
#' Return OO type |
| 172 |
#' @param x Any R object. |
| 173 |
#' @return One of `"bare"` (for non-OO objects), `"S3"`, `"S4"`, |
| 174 |
#' `"R6"`, or `"R7"`. |
| 175 |
#' @noRd |
| 176 |
obj_type_oo <- function(x) { |
| 177 |
if (!is.object(x)) { |
| 178 |
return("bare") |
| 179 |
} |
| 180 |
|
| 181 |
class <- inherits(x, c("R6", "R7_object"), which = TRUE) |
| 182 |
|
| 183 |
if (class[[1]]) { |
| 184 |
"R6" |
| 185 |
} else if (class[[2]]) { |
| 186 |
"R7" |
| 187 |
} else if (isS4(x)) { |
| 188 |
"S4" |
| 189 |
} else { |
| 190 |
"S3" |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#' @param x The object type which does not conform to `what`. Its |
| 195 |
#' `obj_type_friendly()` is taken and mentioned in the error message. |
| 196 |
#' @param what The friendly expected type. |
| 197 |
#' @param ... Arguments passed to [abort()]. |
| 198 |
#' @inheritParams args_error_context |
| 199 |
#' @noRd |
| 200 |
stop_input_type <- function(x, |
| 201 |
what, |
| 202 |
..., |
| 203 |
arg = rlang::caller_arg(x), |
| 204 |
call = rlang::caller_env()) { |
| 205 |
# From compat-cli.R |
| 206 |
format_arg <- rlang::env_get( |
| 207 |
nm = "format_arg", |
| 208 |
last = topenv(), |
| 209 |
default = NULL, |
| 210 |
inherit = TRUE |
| 211 |
) |
| 212 |
if (!is.function(format_arg)) { |
| 213 |
format_arg <- function(x) sprintf("`%s`", x) |
| 214 |
} |
| 215 |
|
| 216 |
message <- sprintf( |
| 217 |
"%s must be %s, not %s.", |
| 218 |
format_arg(arg), |
| 219 |
what, |
| 220 |
obj_type_friendly(x) |
| 221 |
) |
| 222 |
|
| 223 |
rlang::abort(message, ..., call = call, arg = arg) |
| 224 |
} |
| 225 |
|
| 226 |
# nocov end |