diff --git a/NEWS.md b/NEWS.md index 4cd68d15c..c3a7a281a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * The `ard_stack(by)` argument has been renamed to `".by"` and its location moved to after the dots inputs, e.g. `ard_stack(..., .by)`. (#243) +* Bug fix in `ard_stack()` when calls to functions were namespaced. (#242) + * Added `check_ard_structure(column_order, method)` arguments to the function to check for column ordering and whether result contains a `stat_name='method'` row. * Converting `ard_*()` functions and other helpers to S3 generics to make them extendable. (#227) diff --git a/R/ard_stack.R b/R/ard_stack.R index d7f04afde..c4117b2e5 100644 --- a/R/ard_stack.R +++ b/R/ard_stack.R @@ -169,11 +169,25 @@ ard_stack <- function(data, lapply( dots, function(x) { - x_rhs <- f_rhs(x) - x_fn <- call_name(x_rhs) - x_args <- call_args(x_rhs) - - do.call(x_fn, c(list(data = data, by = .by), x_args), envir = attr(x, ".Environment")) + if (!is_call_simple(x)) { + cli::cli_abort( + "{.fun cards::ard_stack} works with {.help [simple calls](rlang::call_name)} + and {.code {as_label(x)}} is not simple.", + call = get_cli_abort_call() + ) + } + x_ns <- call_ns(x) + x_fn <- call_name(x) + x_args <- call_args(x) + + # if a function was namespaced, then grab function from that pkg's Namespace + # styler: off + final_fn <- + if (is.null(x_ns)) x_fn + else get(x_fn, envir = asNamespace(x_ns)) + # styler: on + + do.call(final_fn, c(list(data = data, by = .by), x_args), envir = attr(x, ".Environment")) } ) } diff --git a/inst/WORDLIST b/inst/WORDLIST index c53dc76ed..f93720d18 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -27,6 +27,7 @@ httr jsonlite mis nalysis +namespaced nonmiss pre quosures diff --git a/tests/testthat/_snaps/ard_stack.md b/tests/testthat/_snaps/ard_stack.md index b7bdfd1df..0fdaddeb0 100644 --- a/tests/testthat/_snaps/ard_stack.md +++ b/tests/testthat/_snaps/ard_stack.md @@ -13,3 +13,14 @@ Message i 2 more variables: warning, error +# ard_stack() complex call error + + Code + complex_call <- list() + complex_call$ard_continuous <- ard_continuous + ard_stack(data = mtcars, .by = am, complex_call$ard_continuous(variables = "mpg"), + ) + Condition + Error in `ard_stack()`: + ! `cards::ard_stack()` works with simple calls (`?rlang::call_name()`) and `complex_call$ard_continuous(variables = "mpg")` is not simple. + diff --git a/tests/testthat/test-ard_stack.R b/tests/testthat/test-ard_stack.R index e1907f60b..369f3e503 100644 --- a/tests/testthat/test-ard_stack.R +++ b/tests/testthat/test-ard_stack.R @@ -190,6 +190,20 @@ test_that("ard_stack() .shuffle argument", { ) }) + +test_that("ard_stack() works with namespaced functions", { + expect_equal( + ard_stack( + data = mtcars, + cards::ard_continuous(variables = "mpg") + ), + ard_stack( + data = mtcars, + ard_continuous(variables = "mpg") + ) + ) +}) + test_that("ard_stack() messaging", { expect_snapshot( ard_stack( @@ -200,3 +214,18 @@ test_that("ard_stack() messaging", { head(1L) ) }) + +test_that("ard_stack() complex call error", { + expect_snapshot( + { + complex_call <- list() + complex_call$ard_continuous <- ard_continuous + ard_stack( + data = mtcars, + .by = am, + complex_call$ard_continuous(variables = "mpg"), + ) + }, + error = TRUE + ) +})