Просмотр исходного кода

Set fig.showtext chunk option on package load and error loudly if fig.showtext = FALSE for a chunk with theme_xaringan()

tags/v0.3.0
Garrick Aden-Buie 6 лет назад
Родитель
Сommit
1e6e51422c
3 измененных файлов: 37 добавлений и 1 удалений
  1. +16
    -1
      R/ggplot2.R
  2. +4
    -0
      R/utils.R
  3. +17
    -0
      R/zzz.R

+ 16
- 1
R/ggplot2.R Просмотреть файл

@@ -742,7 +742,9 @@ register_font <- function(
"Font '{family}' must be manually registered using `sysfonts::font_add()`."
)
}
warning(msg, call. = FALSE)
warning(str_wrap(msg), call. = FALSE)
} else {
verify_fig_showtext(fn)
}
}
xaringanthemer_env[["registered_font_families"]] <- c(
@@ -752,6 +754,19 @@ register_font <- function(
family
}

verify_fig_showtext <- function(fn = "theme_xaringan_base") {
if (is.null(knitr::current_input())) return()
# Try to set fig.showtext automatically
if (isTRUE(knitr::opts_current$get("fig.showtext"))) {
return()
}
stop(str_wrap(
"To use ", fn, "() with knitr, you need to set the chunk option ",
"`fig.showtext = TRUE` for this chunk. Or you can set this option ",
"globally with `knitr::opts_chunk$set(fig.showtext = TRUE)`."
))
}

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

+ 4
- 0
R/utils.R Просмотреть файл

@@ -115,3 +115,7 @@ quote_elements_w_spaces <- function(x) {
x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'")
paste(x, collapse = ", ")
}

str_wrap <- function(...) {
paste(strwrap(paste0(...)), collapse = "\n")
}

+ 17
- 0
R/zzz.R Просмотреть файл

@@ -0,0 +1,17 @@
# nocov start
.onLoad <- function(libname, pkgname, ...) {

if ("knitr" %in% loadedNamespaces()) {
knitr::opts_chunk$set(fig.showtext = TRUE)
}

setHook(
packageEvent("knitr", "onLoad"),
function(...) {
knitr::opts_chunk$set(fig.showtext = TRUE)
}
)

invisible()
}
# nocov end

Загрузка…
Отмена
Сохранить