Skip to content

Commit

Permalink
Merge pull request #1813 from LukasWallrich/rlang_funs
Browse files Browse the repository at this point in the history
Add support for rlang anonymous function to fmt() - closes #1762
  • Loading branch information
rich-iannone authored Aug 8, 2024
2 parents aebc2b1 + 99cf47f commit 9394c71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
10 changes: 8 additions & 2 deletions R/fmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
#'
#' `function|list of functions` // **required**
#'
#' Either a single formatting function or a named list of functions.
#' Either a single formatting function or a named list of functions. Can also
#' be anonymous functions, in both base R (`\(x) x + 1`) and `rlang`
#' (`~.x + 1`) syntax.
#'
#'
#' @return An object of class `gt_tbl`.
#'
Expand Down Expand Up @@ -124,10 +127,13 @@ fmt <- function(

# If a single function is supplied to `fns` then
# repackage that into a list as the `default` function
if (is.function(fns)) {
if (!is.list(fns)) {
fns <- list(default = fns)
}

# Convert the `fns` with rlang, so that purrr-style works
fns <- lapply(fns, rlang::as_function)

# Create the `formatter_list`, which is a bundle of
# formatting functions for specific columns and rows
formatter_list <-
Expand Down
4 changes: 3 additions & 1 deletion man/fmt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-fmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ test_that("fmt() works with conditional `rows`", {
)
})

test_that("fmt() works when providing a purrr formula (#1762)", {
# Function has a different class, so that will differ
# - but makes sense since the passed function is actually different
expect_equal(
{v1 <- mtcars %>% gt() %>% fmt(mpg, fns = ~.x+1); v1$`_formats`[[1]]$func$default <- NULL; v1},
{v2 <- mtcars %>% gt() %>% fmt(mpg, fns = function(x) x+1); v2$`_formats`[[1]]$func$default <- NULL; v2}
)
})

test_that("filter_table_to_value() works correctly", {

# Expect that filtering the `locales` table with
Expand Down

0 comments on commit 9394c71

Please sign in to comment.