Skip to content

Commit

Permalink
Hopefully fixes error when ggplot2 is not loaded
Browse files Browse the repository at this point in the history
ref #21
  • Loading branch information
eliocamp committed Feb 13, 2020
1 parent c444ba8 commit 7adc741
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -14,5 +14,7 @@ LazyData: true
Imports:
ggplot2 (>= 3.0.0),
stringi
RoxygenNote: 6.1.1
RoxygenNote: 7.0.2
Roxygen: list(markdown = TRUE)
Suggests:
testthat
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -6,3 +6,6 @@ export(new_scale_color)
export(new_scale_colour)
export(new_scale_fill)
importFrom(ggplot2,ggplot_add)
importFrom(ggplot2,guide_colorbar)
importFrom(ggplot2,guide_colourbar)
importFrom(ggplot2,guide_legend)
6 changes: 6 additions & 0 deletions NEWS.md
@@ -1,3 +1,9 @@
# 0.5.9000

## Bugfixes

* Fixed error when adding a scale with `ggplot2` not loaded. (#21, thanks @rmendels)

# 0.4.0 - G minor

## Bugfixes
Expand Down
4 changes: 2 additions & 2 deletions R/new-scale.R
Expand Up @@ -143,6 +143,7 @@ bump_aes_scales <- function(scales, new_aes) {
lapply(scales, bump_aes_scale, new_aes = new_aes)
}

#' @importFrom ggplot2 guide_colourbar guide_colorbar guide_legend
bump_aes_scale <- function(scale, new_aes) {
old_aes <- scale$aesthetics[remove_new(scale$aesthetics) %in% new_aes]
if (length(old_aes) != 0) {
Expand All @@ -153,8 +154,7 @@ bump_aes_scale <- function(scale, new_aes) {
no_guide <- isFALSE(scale$guide) | isTRUE(scale$guide == "none")
if (!no_guide) {
if (is.character(scale$guide)) {

scale$guide <- match.fun(paste("guide_", scale$guide, sep = ""))()
scale$guide <- get(paste0("guide_", scale$guide), mode = "function")()
}
scale$guide$available_aes[scale$guide$available_aes %in% old_aes] <- new_aes
}
Expand Down
4 changes: 2 additions & 2 deletions man/ggnewscale-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat.R
@@ -0,0 +1,4 @@
library(testthat)
library(ggnewscale)

test_check("ggnewscale")
15 changes: 15 additions & 0 deletions tests/testthat/test-newscale.R
@@ -0,0 +1,15 @@


topography <- expand.grid(x = 1:nrow(volcano),
y = 1:ncol(volcano))
topography$z <- c(volcano)

test_that("works when ggplot2 not loaded", {

g <- ggplot2::ggplot(mapping = ggplot2::aes(x, y)) +
ggplot2::geom_contour(data = topography, ggplot2::aes(z = z, color = ..level..)) +
new_scale_color()

expect_true(inherits(g,"ggplot"))

})

0 comments on commit 7adc741

Please sign in to comment.