From 0b9d58387837db56cabc00cf072c5eafb6966d42 Mon Sep 17 00:00:00 2001 From: Ernest Guevarra Date: Thu, 29 Feb 2024 05:49:01 +0000 Subject: [PATCH] re-factor print function --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/oxfordtheme.R | 1 + R/utils.R | 17 +++++++++++------ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 455efea..b881b72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,8 @@ License: GPL (>= 3) Depends: R (>= 2.10) Imports: - stringr + stringr, + withr Suggests: covr, sf diff --git a/NAMESPACE b/NAMESPACE index b2437f4..bd6db72 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,3 +18,4 @@ importFrom(graphics,par) importFrom(graphics,rect) importFrom(graphics,text) importFrom(stringr,str_pad) +importFrom(withr,with_par) diff --git a/R/oxfordtheme.R b/R/oxfordtheme.R index fc2b05d..b242b13 100644 --- a/R/oxfordtheme.R +++ b/R/oxfordtheme.R @@ -13,5 +13,6 @@ #' @importFrom stringr str_pad #' @importFrom grDevices col2rgb rgb colorRampPalette #' @importFrom graphics rect par image text +#' @importFrom withr with_par #' "_PACKAGE" diff --git a/R/utils.R b/R/utils.R index 37008e7..5dc0fd8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -43,12 +43,17 @@ cmyk2rgb <- function(C, M, Y, K) { #' print.palette <- function(x, ...) { n <- length(x) - old <- par(mar = c(0.5, 0.5, 0.5, 0.5)) - on.exit(par(old)) + # old <- par(mar = c(0.5, 0.5, 0.5, 0.5)) + # on.exit(par(old)) - image(1:n, 1, as.matrix(1:n), col = x, - ylab = "", xaxt = "n", yaxt = "n", bty = "n") + withr::with_par( + new = list(mar = c(0.5, 0.5, 0.5, 0.5)), + code = { + image(1:n, 1, as.matrix(1:n), col = x, + ylab = "", xaxt = "n", yaxt = "n", bty = "n") - rect(0, 0.9, n + 1, 1.1, col = rgb(1, 1, 1, 0.8), border = NA) - text((n + 1) / 2, 1, labels = attr(x, "name"), cex = 1, family = "serif") + rect(0, 0.9, n + 1, 1.1, col = rgb(1, 1, 1, 0.8), border = NA) + text((n + 1) / 2, 1, labels = attr(x, "name"), cex = 1, family = "serif") + } + ) }