Skip to content

Commit

Permalink
remove the ggplot2 version requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Sep 16, 2024
1 parent 9cbbe7a commit 0d42a19
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ License: MIT + file LICENSE
URL: https://github.com/Yunuuuu/ggalign, https://yunuuuu.github.io/ggalign/
BugReports: https://github.com/Yunuuuu/ggalign/issues
Depends:
ggplot2 (>= 3.3.0)
ggplot2
Imports:
cli,
ggh4x,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,4 @@ importFrom(stats,reorder)
importFrom(utils,getFromNamespace)
importFrom(utils,modifyList)
importFrom(utils,packageName)
importFrom(utils,packageVersion)
36 changes: 9 additions & 27 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,19 @@ align_theme <- function(direction, ...) {
)
}

#' @importFrom utils getFromNamespace
#' @importFrom ggplot2 theme_get
complete_theme <- function(theme) {
tryCatch(
getFromNamespace("ggplot2", "complete_theme")(theme),
error = function(cnd) {
if (!is_theme_complete(theme)) {
theme <- theme_get() + theme
if (!is_theme_complete(theme)) {
theme <- tryCatch(
getFromNamespace("ggplot2", "complete_theme")(theme),
error = function(cnd) {
theme_get() + theme
}
theme
}
)
)
}
theme
}

# check whether theme is complete
is_theme_complete <- function(x) isTRUE(attr(x, "complete", exact = TRUE))

# Should rely on ggplot2 >= 3.3.0
#' @importFrom ggplot2 register_theme_elements el_def
theme_elements <- function() {
register_theme_elements(
element_tree = list(
plot.patch_title = el_def("element_text", "text"),
plot.patch_title.top = el_def("element_text", "text"),
plot.patch_title.left = el_def("element_text", "text"),
plot.patch_title.bottom = el_def("element_text", "text"),
plot.patch_title.right = el_def("element_text", "text"),
plot.patch_title.position = el_def("character"),
plot.patch_title.position.top = el_def("character"),
plot.patch_title.position.left = el_def("character"),
plot.patch_title.position.bottom = el_def("character"),
plot.patch_title.position.right = el_def("character")
)
)
}
20 changes: 19 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
# Should rely on ggplot2 >= 3.3.0
##' @importFrom ggplot2 register_theme_elements el_def
#' @importFrom utils packageVersion
.onLoad <- function(libname, pkgname) {
theme_elements()
if (packageVersion("ggplot2") >= "3.3.0") {
register_theme_elements(
element_tree = list(
plot.patch_title = el_def("element_text", "text"),
plot.patch_title.top = el_def("element_text", "text"),
plot.patch_title.left = el_def("element_text", "text"),
plot.patch_title.bottom = el_def("element_text", "text"),
plot.patch_title.right = el_def("element_text", "text"),
plot.patch_title.position = el_def("character"),
plot.patch_title.position.top = el_def("character"),
plot.patch_title.position.left = el_def("character"),
plot.patch_title.position.bottom = el_def("character"),
plot.patch_title.position.right = el_def("character")
)
)
}
invisible()
}

0 comments on commit 0d42a19

Please sign in to comment.