-
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhelpers-shared.R
More file actions
61 lines (58 loc) · 1.85 KB
/
helpers-shared.R
File metadata and controls
61 lines (58 loc) · 1.85 KB
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
56
57
58
59
60
61
#' Check for suggested package with `requireNamespace` and throw error if necessary
#'
#' @noRd
#' @param pkg Package name as a string.
#' @param min_version Optionally, a minimum version number as a string.
#' @return `TRUE`, invisibly, if no error is thrown.
#'
suggested_package <- function(pkg, min_version = NULL) {
stopifnot(length(pkg) == 1, is.character(pkg))
if (!requireNamespace(pkg, quietly = TRUE)) {
abort(paste("Please install the", pkg, "package to use this function."))
}
if (!is.null(min_version)) {
stopifnot(is.character(min_version))
if (utils::packageVersion(pkg) < package_version(min_version)) {
abort(paste(
"Version >= ", min_version, "of the",
pkg, "package is required to use this function."
))
}
}
invisible(TRUE)
}
# Return x if not NULL, otherwise y
`%||%` <- function(x, y) if (!is.null(x)) x else y
#' Warn about ignored arguments
#'
#' @param ... The `...` arguments from the calling function.
#' @param ok_args A character vector of argument names to ignore.
#' @return Nothing, but a warning may be thrown.
#' @noRd
check_ignored_arguments <- function(..., ok_args = character()) {
dots <- list(...)
nms <- names(dots)
if (length(dots)) {
unrecognized <- if (!length(ok_args)) nms else setdiff(nms, ok_args)
if (length(unrecognized)) {
warn(paste(
"The following arguments were unrecognized and ignored:",
paste(unrecognized, collapse = ", ")
))
}
}
}
#' Validate bounds passed to stat_density/geom_density wrappers
#' @noRd
validate_density_bounds <- function(bounds) {
if (is.null(bounds)) {
return(NULL)
}
if (!is.numeric(bounds) || length(bounds) != 2 || anyNA(bounds)) {
abort("`bounds` must be a numeric vector of length 2.")
}
if (bounds[1] >= bounds[2]) {
abort("`bounds` must satisfy bounds[1] < bounds[2].")
}
bounds
}