Skip to content

Move build-time calculations to .onLoad() #3275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/grob-null.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#' @export
zeroGrob <- function() .zeroGrob

.zeroGrob <- grob(cl = "zeroGrob", name = "NULL")
# Will get assigned in .onLoad()
.zeroGrob <- NULL

#' @export
#' @method widthDetails zeroGrob
widthDetails.zeroGrob <- function(x) unit(0, "cm")
Expand Down
2 changes: 1 addition & 1 deletion R/theme-current.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @include theme-defaults.r
#' @include theme-elements.r
ggplot_global$theme_current <- theme_gray()
NULL

#' Get, set, and modify the active theme
#'
Expand Down
4 changes: 4 additions & 0 deletions R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
.onLoad <- function(...) {
backport_unit_methods()

.zeroGrob <<- grob(cl = "zeroGrob", name = "NULL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the documentation to .onLoad() correctly (https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-hooks.html), this should be grid::grob.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think so - where do you see that? .onLoad() is called after the namespace is loaded

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was commenting based on the following sentence. It's possible I misinterpreted it.

Note that the code in .onLoad and .onUnload should not assume any package except the base package is on the search path. Objects in the current package will be visible (unless this is circumvented), but objects from other packages should be imported or the double colon operator should be used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a throwback to ancient times when packages could assume default packages were loaded (e.g. stats) and thus didn't need to be imported in the namespace file... grid is imported by ggplot2 so it's all good


ggplot_global$theme_current <- theme_gray()

# To avoid namespace clash with dplyr.
# It seems surprising that this hack works
if (requireNamespace("dplyr", quietly = TRUE)) {
Expand Down