Skip to content

Commit

Permalink
double-check for grouped_df in group_vars_
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Feb 29, 2024
1 parent 2db2aef commit 2a3d9d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 7 additions & 2 deletions R/compat.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
# dplyr -------------------------------------------------------------------
#' Wrapper around dplyr::group_vars()
#' @noRd
group_vars_ = function(x) {
group_vars_ = function(x, call = caller_env()) {
if (requireNamespace("dplyr", quietly = TRUE)) {
dplyr::group_vars(x)
} else {
# can't have grouped data frames without dplyr, so if dplyr isn't installed
# assume we aren't getting grouped dfs
# we most likely aren't getting grouped dfs --- but still double-check and
# fail in the most common case, grouped_df (maybe someone loaded an rds of a
# grouped df into a setup without dplyr or something...)
if (inherits(x, "grouped_df")) {
stop_not_installed("dplyr", context = "Using grouped data frames", call = call)
}
character()
}
}
Expand Down
24 changes: 14 additions & 10 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,27 @@ warning0 = function(...) {
#' @param context beginning of error message giving the context (e.g. a
#' package name and/or argument combination) that requires the package.
#' @param ... additional messages placed into the `cli_abort()` message.
#' @importFrom rlang caller_env
#' @noRd
stop_if_not_installed = function(packages, context = "This functionality", ..., call = caller_env()) {
for (package in packages) {
if (!requireNamespace(package, quietly = TRUE)) {
cli_abort(
c(
paste0(context, ' requires the {.pkg {package}} package.'),
">" = 'Install the {.pkg {package}} package with {.run install.packages("{package}")}',
...
),
class = "ggdist_missing_package",
ggdist_package = package,
call = call
)
stop_not_installed(package, context = context, ..., call = call)
}
}
}
stop_not_installed = function(package, context = "This functionality", ..., call = caller_env()) {
cli_abort(
c(
paste0(context, ' requires the {.pkg {package}} package.'),
">" = 'Install the {.pkg {package}} package with {.run install.packages("{package}")}',
...
),
class = "ggdist_missing_package",
ggdist_package = package,
call = call
)
}

# get all variable names from an expression
# based on http://adv-r.had.co.nz/dsl.html
Expand Down

0 comments on commit 2a3d9d9

Please sign in to comment.