Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

113 lines
3.2KB

  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(..., .f, .args = NULL) {
  12. mapply(.f, ..., MoreArgs = compact(.args), SIMPLIFY = FALSE, USE.NAMES = TRUE)
  13. }
  14. compact <- function(x) {
  15. x[!vapply(x, is.null, logical(1))]
  16. }
  17. tag_function <- function(.tag = "div") {
  18. function(...) htmltools::tag(.tag, list(...))
  19. }
  20. tag_div <- tag_function("div")
  21. tag_p <- tag_function("p")
  22. tag_a <- tag_function("a")
  23. validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name = "") {
  24. if (!is.null(value) && length(value)) {
  25. value_name <- if (nchar(value_name) > 0) glue("`{value_name}` - ") else ""
  26. warnings <- c()
  27. not_in_choices <- setdiff(value, choices)
  28. if (length(not_in_choices)) {
  29. warnings <- glue("{value_name}Ignoring invalid choices: ",
  30. "\"{paste(not_in_choices, collapse = '\", \"')}\"")
  31. value <- intersect(value, choices)
  32. }
  33. if (!several.ok && length(value) > 1) {
  34. warnings <- c(
  35. warnings,
  36. glue("{value_name}Using the first of {length(value)} values: {value[1]}")
  37. )
  38. value <- value[1]
  39. }
  40. if (length(value)) {
  41. if (length(warnings)) {
  42. rlang::warn(paste(warnings, collapse = "\n"))
  43. }
  44. value
  45. } else {
  46. rlang::abort(glue("{value_name}Must be one of the following valid choices: ",
  47. "\"{paste(choices, collapse = '\", \"')}\""))
  48. }
  49. } else if (!is.null(value) && !length(value)) NULL
  50. }
  51. is_html <- function(x) inherits(class(x), "html")
  52. is_tagList <- function(x) inherits(class(x), "shiny.tag.list")
  53. is_tag <- function(x) inherits(class(x), "shiny.tag")
  54. is_htmlish <- function(x) is_html(x) | is_tag(x) | is_tagList(x)
  55. c_is <- function(x) {
  56. if (is.null(x)) return(NULL)
  57. str_trim(paste("is-", x, sep = "", collapse = " "))
  58. }
  59. c_has <- function(x) {
  60. if (is.null(x)) return(NULL)
  61. str_trim(paste("has-", x, sep = "", collapse = " "))
  62. }
  63. c_prefix <- function(x = NULL, prefix = NULL) {
  64. if (is.null(x)) return(NULL)
  65. paste0(prefix, x)
  66. }
  67. c_str <- function(...) {
  68. str_trim(paste(..., sep = " ", collapse = " "))
  69. }
  70. str_trim <- function(x) {
  71. x <- gsub("^\\s*|\\s*$", "", x)
  72. gsub("\\s+", " ", x)
  73. }
  74. #' Font Awesome Icon
  75. #'
  76. #' Create the correct Font Awesome class.
  77. #' @param name Name of the Font Awesome icon
  78. #' @param solid Should the solid or the regular icon be used?
  79. #' @param as_html If `FALSE` (default), only the icon class is returned.
  80. #' @examples
  81. #' fa_icon("github")
  82. #' fa_icon("star")
  83. #' fa_icon("star", FALSE)
  84. #'
  85. #' @references <https://fontawesome.com/icons>
  86. #' @export
  87. fa_icon <- function(name, solid = TRUE, as_html = FALSE) {
  88. iconClass <- if (name %in% font_awesome_brands) "fab" else {
  89. if (solid) "fas" else "far"
  90. }
  91. iconClass <- paste(iconClass, paste0("fa-", name))
  92. if (!as_html) return(iconClass)
  93. icon <- tag_function("i")(class = iconClass)
  94. htmltools::htmlDependencies(icon) <- rmarkdown::html_dependency_font_awesome()
  95. icon
  96. }