瀏覽代碼

Fix map_arg and other utilities

- compact() returns NULL if all elements are NULL
- map_arg handles no .args provided
- is_htmlish() actually works now
master
Garrick Aden-Buie 7 年之前
父節點
當前提交
e73d5ac964
共有 2 個文件被更改,包括 33 次插入5 次删除
  1. +11
    -5
      R/utils.R
  2. +22
    -0
      tests/testthat/test-utils.R

+ 11
- 5
R/utils.R 查看文件

@@ -12,11 +12,17 @@ dots2list <- function(...) {
}

map_arg <- function(..., .f, .args = NULL) {
mapply(.f, ..., MoreArgs = compact(.args), SIMPLIFY = FALSE, USE.NAMES = TRUE)
.args <- compact(.args)
if (is.null(.args)) {
mapply(.f, ..., SIMPLIFY = FALSE, USE.NAMES = TRUE)
} else {
mapply(.f, ..., MoreArgs = compact(.args), SIMPLIFY = FALSE, USE.NAMES = TRUE)
}
}

compact <- function(x) {
x[!vapply(x, is.null, logical(1))]
x <- x[!vapply(x, is.null, logical(1))]
if (length(x)) x else NULL
}

tag_function <- function(.tag = "div") {
@@ -57,9 +63,9 @@ validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name
} else if (!is.null(value) && !length(value)) NULL
}

is_html <- function(x) inherits(class(x), "html")
is_tagList <- function(x) inherits(class(x), "shiny.tag.list")
is_tag <- function(x) inherits(class(x), "shiny.tag")
is_html <- function(x) inherits(x, "html")
is_tagList <- function(x) inherits(x, "shiny.tag.list")
is_tag <- function(x) inherits(x, "shiny.tag")
is_htmlish <- function(x) is_html(x) | is_tag(x) | is_tagList(x)

c_is <- function(x) {

+ 22
- 0
tests/testthat/test-utils.R 查看文件

@@ -0,0 +1,22 @@
context("test-utils")

test_that("map_arg", {
expect_equal(map_arg("a", "b", "c", .f = paste), list(a = "a b c"))
expect_equal(map_arg("a", "b", "c", .f = paste, .args = list(sep = ",")),
list(a = "a,b,c"))
expect_equal(map_arg("a", "b", "c", .f = paste, .args = list(sep = ",", collapse = NULL)),
list(a = "a,b,c"))
})

test_that("compact", {
expect_equal(compact(list(x = NULL, y = 1)), list(y = 1))
expect_equal(compact(list(x = list(NULL), y = 1)), list(x = list(NULL), y = 1))
expect_null(compact(list(x = NULL, y = NULL)))
})

test_that("is_htmlish", {
expect_true(is_html(htmltools::HTML("test")))
expect_true(is_tagList(htmltools::tagList("test")))
expect_true(is_tag(htmltools::tag("div", list("test"))))
expect_true(is_htmlish(tag_p("test")))
})

Loading…
取消
儲存