😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

118 lines
3.1KB

  1. `%||%` <- function(x, y) if (is.null(x)) y else x
  2. `%??%` <- function(x, y) if (!is.null(x)) y else NULL
  3. requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
  4. raise <- if (required) stop else warning
  5. if (!requireNamespace(pkg, quietly = TRUE)) {
  6. msg <- paste0(
  7. "`",
  8. pkg,
  9. "` is ",
  10. if (required) "required " else "suggested ",
  11. if (fn != "") paste0("by ", fn, "() ")[1],
  12. "but is not installed."
  13. )
  14. raise(msg, call. = FALSE)
  15. return(invisible(FALSE))
  16. }
  17. invisible(TRUE)
  18. }
  19. #' @keywords internal
  20. call_style_xaringan <- function() {
  21. paste0(
  22. "style_xaringan(",
  23. paste(names(formals(style_xaringan)), collapse = ", "),
  24. ")"
  25. )
  26. }
  27. #' Specify Google Font
  28. #'
  29. #' Builds Google Fonts URL from family name. Extra weights are given in the
  30. #' `...` parameters. Languages can be specified in `langauges` and must one or
  31. #' more of the language codes as given by `google_language_codes()`.
  32. #'
  33. #' @examples
  34. #' google_font("Josefin Sans", "400", "400i", "600i", "700")
  35. #' google_font("Josefin Sans", languages = c("latin-ext", "vietnamese"))
  36. #' @param family Font family
  37. #' @param ... Font weights to include, example "400", "400i"
  38. #' @param languages Font languages to include (dependent on the font.) See
  39. #' [google_language_codes()].
  40. #' @export
  41. google_font <- function(family, ..., languages = NULL) {
  42. base <- "https://fonts.googleapis.com/css?family="
  43. weights <- if (length(list(...))) paste(c(...), collapse = ",")
  44. languages <- if (!is.null(languages)) paste(google_language_codes(languages), collapse = ",")
  45. structure(
  46. list(
  47. family = family,
  48. weights = weights,
  49. languages = languages,
  50. url = paste0(
  51. base,
  52. gsub(" ", "+", family),
  53. if (!is.null(weights)) paste0(":", weights),
  54. if (!is.null(languages)) paste0("&subset=", languages),
  55. "&display=swap"
  56. )
  57. ),
  58. class = "google_font"
  59. )
  60. }
  61. #' @title List Valid Google Language Codes
  62. #' @description Gives a list of valid Language Codes for Google Fonts, or
  63. #' validates that the language codes given are valid.
  64. #' @seealso [google_font()]
  65. #' @param language_codes Vector of potential Google language codes
  66. #' @export
  67. google_language_codes <- function(
  68. language_codes = c(
  69. "latin",
  70. "latin-ext",
  71. "sinhala",
  72. "greek",
  73. "hebrew",
  74. "vietnamese",
  75. "cyrillic",
  76. "cyrillic-ext",
  77. "devanagari",
  78. "arabic",
  79. "khmer",
  80. "tamil",
  81. "greek-ext",
  82. "thai",
  83. "bengali",
  84. "gujarati",
  85. "oriya",
  86. "malayalam",
  87. "gurmukhi",
  88. "kannada",
  89. "telugu",
  90. "myanmar"
  91. )) {
  92. unique(match.arg(language_codes, several.ok = TRUE))
  93. }
  94. print.google_font <- function(x) {
  95. cat(
  96. "Family: ",
  97. x$family,
  98. if (!is.null(x$weights)) paste("\nWeights:", x$weights),
  99. if (!is.null(x$languages)) paste("\nLangs: ", x$languages),
  100. "\nURL: ",
  101. x$url
  102. )
  103. }
  104. quote_elements_w_spaces <- function(x) {
  105. x <- strsplit(x, ", ?")[[1]]
  106. has_space <- grepl("\\w \\w", x)
  107. not_quoted <- grepl("^\\w.+\\w$", x)
  108. x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'")
  109. paste(x, collapse = ", ")
  110. }