From c0f539cdd88b2915e75e6e9833db02b06788c3cd Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 1 Feb 2024 16:58:41 +0100 Subject: [PATCH 1/5] In case quarto is not available produce empty vignette This is based on knitr experience --- R/utils-vignettes.R | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/R/utils-vignettes.R b/R/utils-vignettes.R index 80810f7..cc6fef5 100644 --- a/R/utils-vignettes.R +++ b/R/utils-vignettes.R @@ -18,10 +18,24 @@ vig_engine <- function(..., quarto_format) { } vweave_quarto <- function(format) { + # protect if Quarto is not installed + if (is.null(quarto_path())) { + msg <- c( + x = "Quarto binary is required to build Quarto vignettes but is not available.", + i = "Please make sure it is installed and found by {.code find_quarto()}." + ) + if (is_R_CMD_check()) { + cli::cli_inform(msg) + } else { + cli::cli_abort(msg, call = NULL) + } + return(vweave_empty) + } meta <- get_meta(format) - function(file, driver, syntax, encoding, quiet = FALSE, ...) { + vweave_quarto_ <- function(file, driver, syntax, encoding, quiet = FALSE, ...) { quarto_render(file, ..., output_format = format, metadata = meta) } + return(vweave_quarto_) } get_meta <- function(format) { @@ -45,3 +59,25 @@ get_meta_for_html <- function() { ) meta } + +is_R_CMD_check <- function() { + !is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA)) || + tolower(Sys.getenv("_R_CHECK_LICENSE_")) == "true" +} + +# from knitr internal +is_cran_check <- function() { + is_cran() && is_R_CMD_check() +} + +is_cran = function() { + !interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false"))) +} + +# trick from knitr to avoid problem on R CMD check (e.g. when no Quarto available) +# It will silently skip the vignette +vweave_empty <- function(file, ..., .reason = 'Quarto') { + out <- sprintf("%s.html", tools::file_path_sans_ext(basename(file))) + writeLines(sprintf("The vignette could not be built because %s is not available.", .reason), out) + out +} From 165ed55609b99f3337cab3d5b3d9aa3e59366fb1 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 2 Feb 2024 15:29:36 +0100 Subject: [PATCH 2/5] Insure we don't try to install CTAN package with Quarto on CRAN during R CMD Check --- R/utils-vignettes.R | 14 +++++++++++++- tests/testthat/test-utils-vignettes.R | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-utils-vignettes.R diff --git a/R/utils-vignettes.R b/R/utils-vignettes.R index cc6fef5..d8ee2bd 100644 --- a/R/utils-vignettes.R +++ b/R/utils-vignettes.R @@ -43,6 +43,18 @@ get_meta <- function(format) { if (format == "html") { return(get_meta_for_html()) } + if (format == "pdf") { + return(get_meta_for_pdf()) + } +} + +get_meta_for_pdf <- function() { + meta <- list() + meta$format$pdf <- list( + # don't try to install CTAN package on CRAN environment + `latex-auto-install` = !is_cran_check() + ) + meta } get_meta_for_html <- function() { @@ -71,7 +83,7 @@ is_cran_check <- function() { } is_cran = function() { - !interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false"))) + !rlang::is_interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false"))) } # trick from knitr to avoid problem on R CMD check (e.g. when no Quarto available) diff --git a/tests/testthat/test-utils-vignettes.R b/tests/testthat/test-utils-vignettes.R new file mode 100644 index 0000000..7cab93e --- /dev/null +++ b/tests/testthat/test-utils-vignettes.R @@ -0,0 +1,7 @@ +test_that("don't auto install CTAN package on CRAN", { + skip_if_not_installed("withr") + withr::with_envvar(list("NOT_CRAN" = "false", "_R_CHECK_LICENSE_" = "true"), { + expect_false(get_meta_for_pdf()$format$pdf$`latex-auto-install`) + }) + expect_true(get_meta_for_pdf()$format$pdf$`latex-auto-install`) +}) From a349469d5261a7ad56d4de83549d9b5d28acffc8 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 2 Feb 2024 16:12:27 +0100 Subject: [PATCH 3/5] Check R CMD check later during the vignette building and not during vignette registration --- R/utils-vignettes.R | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/R/utils-vignettes.R b/R/utils-vignettes.R index d8ee2bd..6e03535 100644 --- a/R/utils-vignettes.R +++ b/R/utils-vignettes.R @@ -19,23 +19,22 @@ vig_engine <- function(..., quarto_format) { vweave_quarto <- function(format) { # protect if Quarto is not installed - if (is.null(quarto_path())) { - msg <- c( - x = "Quarto binary is required to build Quarto vignettes but is not available.", - i = "Please make sure it is installed and found by {.code find_quarto()}." - ) - if (is_R_CMD_check()) { - cli::cli_inform(msg) - } else { - cli::cli_abort(msg, call = NULL) - } - return(vweave_empty) - } meta <- get_meta(format) - vweave_quarto_ <- function(file, driver, syntax, encoding, quiet = FALSE, ...) { + function(file, driver, syntax, encoding, quiet = FALSE, ...) { + if (is.null(quarto_path())) { + msg <- c( + "Quarto binary is required to build Quarto vignettes but is not available.", + i = "Please make sure it is installed and found by {.code find_quarto()}." + ) + if (is_R_CMD_check()) { + cli::cli_inform(msg) + } else { + cli::cli_abort(msg, call = NULL) + } + return(vweave_empty(file)) + } quarto_render(file, ..., output_format = format, metadata = meta) } - return(vweave_quarto_) } get_meta <- function(format) { From aaac99a85a5c19fe9a72f29a40e9fdaf26ad1870 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 2 Feb 2024 16:12:53 +0100 Subject: [PATCH 4/5] Update test to work on R CMD Check and skip on cran because unsure it works well - but I need this on CI --- tests/testthat/test-utils-vignettes.R | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-utils-vignettes.R b/tests/testthat/test-utils-vignettes.R index 7cab93e..c1af9c0 100644 --- a/tests/testthat/test-utils-vignettes.R +++ b/tests/testthat/test-utils-vignettes.R @@ -1,7 +1,16 @@ test_that("don't auto install CTAN package on CRAN", { + skip_on_cran() skip_if_not_installed("withr") - withr::with_envvar(list("NOT_CRAN" = "false", "_R_CHECK_LICENSE_" = "true"), { + withr::with_envvar(list( + # simulate non CRAN + "NOT_CRAN" = "false", + # simulate R CMD check + "_R_CHECK_LICENSE_" = "true"), { expect_false(get_meta_for_pdf()$format$pdf$`latex-auto-install`) }) - expect_true(get_meta_for_pdf()$format$pdf$`latex-auto-install`) + withr::with_envvar(list( + "_R_CHECK_PACKAGE_NAME_" = NA + ), { + expect_true(get_meta_for_pdf()$format$pdf$`latex-auto-install`) + }) }) From 9358b7abcfa261cf32eaaf7b222a58b52fb99db6 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 2 Feb 2024 17:20:16 +0100 Subject: [PATCH 5/5] Comment in right place --- R/utils-vignettes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils-vignettes.R b/R/utils-vignettes.R index 6e03535..1d6db77 100644 --- a/R/utils-vignettes.R +++ b/R/utils-vignettes.R @@ -18,9 +18,9 @@ vig_engine <- function(..., quarto_format) { } vweave_quarto <- function(format) { - # protect if Quarto is not installed meta <- get_meta(format) function(file, driver, syntax, encoding, quiet = FALSE, ...) { + # protect if Quarto is not installed if (is.null(quarto_path())) { msg <- c( "Quarto binary is required to build Quarto vignettes but is not available.",