Parcourir la source

Move requires_package to r/utils.R and add tests

tags/v0.3.0
Garrick Aden-Buie il y a 6 ans
Parent
révision
2aecc9fc10
3 fichiers modifiés avec 24 ajouts et 17 suppressions
  1. +0
    -17
      R/ggplot2.R
  2. +17
    -0
      R/utils.R
  3. +7
    -0
      tests/testthat/test-utils.R

+ 0
- 17
R/ggplot2.R Voir le fichier

@@ -709,23 +709,6 @@ register_font <- function(
family
}

requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
raise <- if (required) stop else warning
if (!requireNamespace(pkg, quietly = TRUE)) {
msg <- paste0(
"`",
pkg,
"` is ",
if (required) "required " else "suggested ",
if (fn != "") paste0("by ", fn, "() ")[1],
"but is not installed."
)
raise(msg, call. = FALSE)
invisible(FALSE)
}
invisible(TRUE)
}

requires_xaringanthemer_env <- function(
css_file = NULL,
try_css = TRUE,

+ 17
- 0
R/utils.R Voir le fichier

@@ -74,6 +74,23 @@ is_light_color <- function(x) {
lum[1, 1] > 0.179
}

requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
raise <- if (required) stop else warning
if (!requireNamespace(pkg, quietly = TRUE)) {
msg <- paste0(
"`",
pkg,
"` is ",
if (required) "required " else "suggested ",
if (fn != "") paste0("by ", fn, "() ")[1],
"but is not installed."
)
raise(msg, call. = FALSE)
return(invisible(FALSE))
}
invisible(TRUE)
}

#' @keywords internal
call_style_xaringan <- function() {
paste0(

+ 7
- 0
tests/testthat/test-utils.R Voir le fichier

@@ -4,3 +4,10 @@ test_that("printing google_font works", {
"google-font-print.txt"
)
})

test_that("requires_package()", {
expect_true(requires_package("purrr"))
expect_error(requires_package("notapackage", "myFunction"), "myFunction")
expect_warning(requires_package("notapackage", required = FALSE))
expect_false(requires_package("notapackage", required = FALSE))
})

Chargement…
Annuler
Enregistrer