Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter to account for empty vector inputs like character(0) #306

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#' NULL values with. If `character()` whole output is `character()`. If
#' `NULL` all NULL values are dropped (as in `paste0()`). Otherwise the
#' value is replaced by the value of `.null`.
#' @param .empty \[`character(1)`: \sQuote{character()}]\cr Value to replace
#' vectors of length 0 with. If `NULL` empty vectors are treated as NULL and
#' the behaviours specified in `.null` applies. If `character()` whole output is
#' `character()`. Otherwise the value is replaced by the value of `.empty`.
#' @param .comment \[`character(1)`: \sQuote{#}]\cr Value to use as the comment
#' character.
#' @param .literal \[`boolean(1)`: \sQuote{FALSE}]\cr Whether to treat single or
Expand Down Expand Up @@ -87,7 +91,7 @@
#' @name glue
#' @export
glue_data <- function(.x, ..., .sep = "", .envir = parent.frame(),
.open = "{", .close = "}", .na = "NA", .null = character(),
.open = "{", .close = "}", .na = "NA", .null = character(), .empty = character(),
.comment = "#", .literal = FALSE, .transformer = identity_transformer, .trim = TRUE) {

if (is.null(.envir)) {
Expand Down Expand Up @@ -120,7 +124,12 @@ glue_data <- function(.x, ..., .sep = "", .envir = parent.frame(),
# - If `.null == NULL` then it is allowed and any such argument will be
# silently dropped.
# - In other cases output is treated as it was evaluated to `.null`.
eval(call("force", as.symbol(paste0("..", x)))) %||% .null
eval_func <- eval(call("force", as.symbol(paste0("..", x))))
if (length(eval_func) == 0 & !is.null(eval_func)) {
.empty
} else {
eval_func %||% .null
}
}
)
unnamed_args <- drop_null(unnamed_args)
Expand Down Expand Up @@ -151,7 +160,11 @@ glue_data <- function(.x, ..., .sep = "", .envir = parent.frame(),
}

f <- function(expr){
eval_func <- .transformer(expr, env) %||% .null
eval_func <- .transformer(expr, env)
if (length(eval_func) == 0 & !is.null(eval_func)) {
eval_func <- .empty
}
eval_func <- eval_func %||% .null

# crayon functions *can* be used, so we use tryCatch()
# to give as.character() a chance to work
Expand Down
6 changes: 6 additions & 0 deletions man/glue.Rd

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

70 changes: 70 additions & 0 deletions tests/testthat/test-glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,76 @@ test_that("glue replaces NULL input if `.null` is not NULL or character()", {
"x: foo")
})

test_that("glue always returns character() if given any empty input if `.empty` == character()", {
expect_equal(
glue("{character()}", .empty = character()),
character())

expect_equal(
glue("{numeric()}", .empty = character()),
character())

expect_equal(
glue("{logical()}", .empty = character()),
character())

expect_equal(
glue("{integer()}", 1, .empty = character()),
character())

expect_equal(
glue("{double()}", 1, .empty = character()),
character())

expect_equal(
glue("{complex()}", 1, .empty = character()),
character())

expect_equal(
glue("{raw()}", 1, .empty = character()),
character())
})

test_that("glue always defaults to .null if given any empty input if `.empty` == 'NULL'", {
expect_equal(
glue("{character()}", .empty = NULL, .null = "foo"),
"foo")

expect_equal(
glue("{character()}", .empty = NULL, .null = NULL),
character())

expect_equal(
glue("{character()}", .empty = NULL, .null = character()),
character())

expect_equal(
glue("{character()}foo", .empty = NULL, .null = NULL),
"foo")
})

test_that("glue always returns character() if given any empty input if `.empty` == character()", {
expect_equal(
glue("{character()}", .empty = "foo"),
"foo")

expect_equal(
glue(character(), .empty = "foo"),
"foo")

expect_equal(
glue(character(), 1, .empty = "foo"),
"foo1")

expect_equal(
glue(1, character(), 2, .empty = "foo"),
"1foo2")

expect_equal(
glue("x: {character()}", .empty = "foo"),
"x: foo")
})

test_that("glue works within functions", {
x <- 1
f <- function(msg) glue(msg, .envir = parent.frame())
Expand Down