Przeglądaj źródła

Ensure that text_font reaches theme_xaringan_set_defaults() when set_ggplot_defaults == TRUE

tags/v0.3.0
Garrick Aden-Buie 6 lat temu
rodzic
commit
d8ec41f343
2 zmienionych plików z 32 dodań i 6 usunięć
  1. +15
    -6
      R/ggplot2.R
  2. +17
    -0
      tests/testthat/test-ggplot2.R

+ 15
- 6
R/ggplot2.R Wyświetl plik

@@ -308,15 +308,21 @@ theme_xaringan_base <- function(
get_theme_font("header")
}

text_font %||% "sans"
title_font %||% "sans"
text_font <- text_font %||% "sans"
title_font <- title_font %||% "sans"

if (set_ggplot_defaults) {
accent_color <- accent_color %||% xaringanthemer_env$header_color %||% text_color
accent_secondary_color <- accent_secondary_color %||% xaringanthemer_env$text_bold_color %||% accent_color
accent_color <- full_length_hex(accent_color)
accent_secondary_color <- full_length_hex(accent_secondary_color)
theme_xaringan_set_defaults(text_color, background_color, accent_color, accent_secondary_color)
theme_xaringan_set_defaults(
text_color = text_color,
background_color = background_color,
accent_color = accent_color,
accent_secondary_color = accent_secondary_color,
text_font = text_font
)
}

theme <- ggplot2::theme(
@@ -459,7 +465,6 @@ theme_xaringan_restore_defaults <- function() {
return(invisible())
}


old_default <- xaringanthemer_env$old_ggplot_defaults
old_default_not_std <- vapply(old_default, function(x) length(x) > 0, logical(1))
old_default <- old_default[old_default_not_std]
@@ -476,12 +481,16 @@ theme_xaringan_restore_defaults <- function() {
}

safely_set_geom <- function(geom, new) {
warn <- function(x) {
rlang::warn(x$message)
invisible()
}
tryCatch(
{
ggplot2::update_geom_defaults(geom, new)
},
error = function(e) invisible(),
warning = function(w) invisible()
error = warn,
warning = warn
)
}


+ 17
- 0
tests/testthat/test-ggplot2.R Wyświetl plik

@@ -102,6 +102,23 @@ describe("theme_xaringan()", {
})
)
})

it("correctly overwrites geom default values", {
fonts <- with_clean_session(function() {
style <- xaringanthemer::style_xaringan(outfile = NULL)

g <- ggplot2::ggplot(mtcars, ggplot2::aes(x = cyl, y = disp, label = carb))
fonts <- list()
plot1 <- g + ggplot2::geom_text() + xaringanthemer::theme_xaringan()
fonts[[1]] <- plot1$layers[[1]]$geom$default_aes$family
plot2 <- g + ggplot2::geom_text() + xaringanthemer::theme_xaringan(text_font = "Ranga", text_font_use_google = TRUE)
fonts[[2]] <- plot2$layers[[1]]$geom$default_aes$family
fonts
})

expect_equal(fonts[[1]], "Noto Sans")
expect_equal(fonts[[2]], "Ranga")
})
})

describe("theme_xaringan_inverse()", {

Ładowanie…
Anuluj
Zapisz