Skip to content

Commit a9f5e0a

Browse files
committed
Add test package and lookup fix.
Thanks to Martin Morgan for locating problem
1 parent 0c5fc80 commit a9f5e0a

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

R/scales-.r

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ scales_add_defaults <- function(scales, data, aesthetics, env) {
7575
)
7676
new_aesthetics <- intersect(new_aesthetics, names(datacols))
7777
if (length(datacols) == 0) return()
78-
78+
7979
for(aes in new_aesthetics) {
8080
disc <- is.discrete(datacols[[aes]])
8181
type <- if (disc) "discrete" else "continuous"
8282
scale_name <- paste("scale", aes, type, sep="_")
83-
83+
8484
# Skip aesthetics with no scales (e.g. group, order, etc)
85-
if (!exists(scale_name, globalenv())) next
86-
scale_f <- get(scale_name, globalenv())
85+
scale_f <- find_global(scale_name)
86+
if (is.null(scale_f)) next
8787

8888
if (disc) {
8989
args <- list()
@@ -96,6 +96,21 @@ scales_add_defaults <- function(scales, data, aesthetics, env) {
9696

9797
}
9898

99+
# Look for object first in global environment and if not found, then in
100+
# ggplot2 package environment. This makes it possible to override default
101+
# scales by setting them in the default environment.
102+
find_global <- function(name) {
103+
if (exists(name, globalenv())) {
104+
return(get(name, globalenv()))
105+
}
106+
107+
if (exists(name, "package::ggplot2")) {
108+
return(get(name, "package::ggplot2"))
109+
}
110+
111+
NULL
112+
}
113+
99114
# Determine default transformation for continuous scales
100115
trans_type <- function(x) {
101116
if (inherits(x, "Date")) {

inst/test_ns/DESCRIPTION

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Package: nstest
2+
Type: Package
3+
Title: Test ggplot2 NAMESPACE
4+
Version: 0.1
5+
Author: Hadley Wickham <[email protected]>
6+
Maintainer: Hadley Wickham <[email protected]>
7+
Description: Check to see if ggplot2 namespace set up correctly.
8+
Imports:
9+
ggplot2
10+
License: GPL-2
11+
Collate:
12+
'my-plot.r'

inst/test_ns/NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export(my_plot)
2+
import(ggplot2)

inst/test_ns/R/my-plot.r

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#' Create a plot.
2+
#'
3+
#' @export
4+
#' @import ggplot2
5+
#' @examples
6+
#' plot(my_plot())
7+
my_plot <- function() {
8+
df <- data.frame(x = 1:10, y = sample(10), z = runif(1))
9+
10+
ggplot(df) + geom_point(aes_string(x = "x", y = "y", colour = "z"))
11+
12+
}

inst/test_ns/man/my_plot.Rd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\name{my_plot}
2+
\alias{my_plot}
3+
\title{Create a plot.}
4+
\usage{
5+
my_plot()
6+
}
7+
\description{
8+
Create a plot.
9+
}
10+
\examples{
11+
plot(my_plot())
12+
}
13+

0 commit comments

Comments
 (0)