Skip to content

Commit 27c17d6

Browse files
committed
use "stat" for anon functions. support formulas
1 parent 23865df commit 27c17d6

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

R/bayesplot-helpers.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ overlay_function <- function(...) {
475475
# Resolve a function name and store the expression passed in by the user
476476
#' @noRd
477477
#' @param f a function-like thing: a string naming a function, a function
478-
#' object, an anonymous function object, and `NULL`.
478+
#' object, an anonymous function object, a formula-based lambda, and `NULL`.
479479
#' @param fallback character string providing a fallback function name
480480
#' @return the function named in `f` with an added `"tagged_expr"` attribute
481481
#' containing the expression to represent the function name and an
482482
#' `"is_anonymous_function"` attribute to flag if the expression is a call to
483483
#' `function()`.
484-
as_tagged_function <- function(f, fallback = "func") {
484+
as_tagged_function <- function(f = NULL, fallback = "func") {
485485
qf <- enquo(f)
486486
f <- eval_tidy(qf)
487487
if (!is.null(attr(f, "tagged_expr"))) return(f)
@@ -490,17 +490,17 @@ as_tagged_function <- function(f, fallback = "func") {
490490
f_fn <- f
491491

492492
if (rlang::is_character(f)) { # f = "mean"
493-
# using sym() on the evaluated `f` that a variable that names a
493+
# using sym() on the evaluated `f` means that a variable that names a
494494
# function string `x <- "mean"; as_tagged_function(x)` will be lost
495-
# but that seems okay!
495+
# but that seems okay
496496
f_expr <- rlang::sym(f)
497497
f_fn <- match.fun(f)
498498
} else if (is_null(f)) { # f = NULL
499499
f_fn <- identity
500500
f_expr <- rlang::sym(fallback)
501501
} else if (is_callable(f)) { # f = mean or f = function(x) mean(x)
502-
f_expr <- f_expr
503-
f_fn <- f
502+
f_expr <- f_expr # or f = ~mean(.x)
503+
f_fn <- as_function(f)
504504
}
505505

506506
# Setting attributes on primitive functions is deprecated, so wrap them
@@ -512,7 +512,8 @@ as_tagged_function <- function(f, fallback = "func") {
512512
}
513513

514514
attr(f_fn, "tagged_expr") <- f_expr
515-
attr(f_fn, "is_anonymous_function") <- is_call(f_expr, name = "function")
515+
attr(f_fn, "is_anonymous_function") <- is_call(f_expr, name = "function") ||
516+
is_formula(f_expr)
516517
f_fn
517518
}
518519

R/ppc-errors.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,12 @@ error_label <- function() {
432432
}
433433

434434
error_avg_label <- function(stat = NULL) {
435-
stat <- as_tagged_function({{ stat }}, fallback = "Average")
435+
stat <- as_tagged_function({{ stat }}, fallback = "stat")
436436
e <- attr(stat, "tagged_expr")
437-
de <- deparse1(e)
438437
if (attr(stat, "is_anonymous_function")) {
439-
de <- paste0("(", de, ")")
438+
e <- sym("stat")
440439
}
440+
de <- deparse1(e)
441441
expr(paste((!!de))(italic(y) - italic(y)[rep]))
442442
}
443443

R/ppc-scatterplots.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ ppc_scatter_avg_data <- function(y, yrep, group = NULL, stat = "mean") {
223223
# internal ----------------------------------------------------------------
224224

225225
yrep_avg_label <- function(stat = NULL) {
226-
stat <- as_tagged_function({{ stat }}, fallback = "Average")
226+
stat <- as_tagged_function({{ stat }}, fallback = "stat")
227227
e <- attr(stat, "tagged_expr")
228-
de <- deparse1(e)
229228
if (attr(stat, "is_anonymous_function")) {
230-
de <- paste0("(", de, ")")
229+
e <- sym("stat")
231230
}
231+
de <- deparse1(e)
232232
expr(paste((!!de))(italic(y)[rep]))
233233
}
234234

tests/testthat/test-convenience-functions.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,16 @@ test_that("as_tagged_function handles string input", {
210210
expect_equal(attr(fn, "tagged_expr"), rlang::sym("mean"))
211211
})
212212

213-
test_that("as_tagged_function handles anonymous function", {
213+
test_that("as_tagged_function handles anonymous functions", {
214214
fn <- as_tagged_function(function(x) mean(x^2))
215215
expect_type(fn, "closure")
216216
expect_equal(fn(1:3), mean((1:3)^2))
217217
expect_equal(attr(fn, "tagged_expr"), rlang::expr( function(x) mean(x^2)))
218+
219+
fn <- as_tagged_function(~mean(.x^2))
220+
expect_type(fn, "closure")
221+
expect_equal(fn(1:3), mean((1:3)^2))
222+
expect_equal(attr(fn, "tagged_expr"), rlang::expr( ~mean(.x^2)))
218223
})
219224

220225
test_that("as_tagged_function handles NULL with fallback name", {

0 commit comments

Comments
 (0)