From 2a3d9d96174180754ceaaddc652d1e3bb2bbda5a Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Wed, 28 Feb 2024 20:55:53 -0600 Subject: [PATCH] double-check for grouped_df in group_vars_ --- R/compat.R | 9 +++++++-- R/util.R | 24 ++++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/R/compat.R b/R/compat.R index 0e5eb8d3..4b469612 100755 --- a/R/compat.R +++ b/R/compat.R @@ -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() } } diff --git a/R/util.R b/R/util.R index f0c95f00..576ba37c 100644 --- a/R/util.R +++ b/R/util.R @@ -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