You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1KB

  1. apply_tag <- function(ll, tag, class = NULL, extra_attributes = NULL) {
  2. lapply(ll, function(x) htmltools::tag(tag, c(x, class = class, extra_attributes)))
  3. }
  4. dots2list <- function(...) {
  5. # Convert dots to list, but return first element if the first element is a list
  6. # and the length of ... is 1
  7. x <- list(...)
  8. if (length(x) == 1 && is.list(x[[1]])) return(x[[1]])
  9. x
  10. }
  11. map_arg <- function(x, .f, .args = NULL) {
  12. mapply(.f, x, MoreArgs = .args, SIMPLIFY = FALSE, USE.NAMES = TRUE)
  13. }
  14. tag_function <- function(.tag = "div") {
  15. function(...) htmltools::tag(.tag, list(...))
  16. }
  17. validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name = "") {
  18. if (!is.null(value) && length(value)) {
  19. value_name <- if (nchar(value_name) > 0) glue("`{value_name}` - ") else ""
  20. if (!several.ok && length(value) > 1) {
  21. msg <- glue("{value_name}Using the first of {length(value)} values: {value[1]}")
  22. rlang::warn(msg)
  23. value <- value[1]
  24. }
  25. not_in_choices <- setdiff(value, choices)
  26. if (length(not_in_choices)) {
  27. msg <- glue("{value_name}Ignoring invalid choices: ",
  28. "\"{paste(not_in_choices, collapse = '\", \"')}\"")
  29. rlang::warn(msg)
  30. value <- intersect(value, choices)
  31. }
  32. if (length(value)) {
  33. value
  34. } else {
  35. rlang::abort(glue("{value_name}Must be one of the following valid choices: ",
  36. "\"{paste(choices, collapse = '\", \"')}\""))
  37. }
  38. } else if (!is.null(value) && !length(value)) NULL
  39. }
  40. is_html <- function(x) inherits(class(x), "html")
  41. is_tagList <- function(x) inherits(class(x), "shiny.tag.list")
  42. is_tag <- function(x) inherits(class(x), "shiny.tag")
  43. is_htmlish <- function(x) is_html(x) | is_tag(x) | is_tagList(x)
  44. c_is <- function(x) {
  45. if (is.null(x)) return(NULL)
  46. str_trim(paste("is-", x, sep = "", collapse = " "))
  47. }
  48. c_has <- function(x) {
  49. if (is.null(x)) return(NULL)
  50. str_trim(paste("has-", x, sep = "", collapse = " "))
  51. }
  52. c_prefix <- function(x = NULL, prefix = NULL) {
  53. if (is.null(x)) return(NULL)
  54. paste0(prefix, x)
  55. }
  56. str_trim <- function(x) {
  57. x <- gsub("^\\s*|\\s*$", "", x)
  58. gsub("\\s+", " ", x)
  59. }