😎 Give your xaringan slides some style
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

126 lines
3.3KB

  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 `languages` 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. #' @return A `"google_font"` object.
  41. #' @export
  42. google_font <- function(family, ..., languages = NULL) {
  43. base <- "https://fonts.googleapis.com/css?family="
  44. weights <- if (length(list(...))) paste(c(...), collapse = ",")
  45. languages <- if (!is.null(languages)) paste(google_language_codes(languages), collapse = ",")
  46. structure(
  47. list(
  48. family = family,
  49. weights = weights,
  50. languages = languages,
  51. url = paste0(
  52. base,
  53. gsub(" ", "+", family),
  54. if (!is.null(weights)) paste0(":", weights),
  55. if (!is.null(languages)) paste0("&subset=", languages),
  56. "&display=swap"
  57. )
  58. ),
  59. class = "google_font"
  60. )
  61. }
  62. is_google_font <- function(x) inherits(x, "google_font")
  63. #' @title List Valid Google Language Codes
  64. #' @description Gives a list of valid Language Codes for Google Fonts, or
  65. #' validates that the language codes given are valid.
  66. #' @seealso [google_font()]
  67. #' @param language_codes Vector of potential Google language codes
  68. #' @return A vector of Google Font language codes matching `language_codes`.
  69. #' @export
  70. google_language_codes <- function(
  71. language_codes = c(
  72. "latin",
  73. "latin-ext",
  74. "sinhala",
  75. "greek",
  76. "hebrew",
  77. "vietnamese",
  78. "cyrillic",
  79. "cyrillic-ext",
  80. "devanagari",
  81. "arabic",
  82. "khmer",
  83. "tamil",
  84. "greek-ext",
  85. "thai",
  86. "bengali",
  87. "gujarati",
  88. "oriya",
  89. "malayalam",
  90. "gurmukhi",
  91. "kannada",
  92. "telugu",
  93. "myanmar"
  94. )) {
  95. unique(match.arg(language_codes, several.ok = TRUE))
  96. }
  97. print.google_font <- function(x) {
  98. cat(
  99. "Family: ",
  100. x$family,
  101. if (!is.null(x$weights)) paste("\nWeights:", x$weights),
  102. if (!is.null(x$languages)) paste("\nLangs: ", x$languages),
  103. "\nURL: ",
  104. x$url
  105. )
  106. }
  107. quote_elements_w_spaces <- function(x) {
  108. x <- strsplit(x, ", ?")[[1]]
  109. has_space <- grepl("\\w \\w", x)
  110. not_quoted <- grepl("^\\w.+\\w$", x)
  111. x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'")
  112. paste(x, collapse = ", ")
  113. }
  114. str_wrap <- function(...) {
  115. paste(strwrap(paste0(...)), collapse = "\n")
  116. }