Skip to content

Commit 2ea4a2e

Browse files
committed
supply anchor/domain defaults so we can assume they exist in subplot
1 parent 3770f78 commit 2ea4a2e

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

R/utils.R

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ arrange_safe <- function(data, vars) {
7474
}
7575

7676
is_mapbox <- function(p) {
77-
identical(p$x[["mapType"]], "mapbox")
77+
identical(p$x$layout[["mapType"]], "mapbox")
7878
}
7979

8080
is_geo <- function(p) {
81-
identical(p$x[["mapType"]], "geo")
81+
identical(p$x$layout[["mapType"]], "geo")
8282
}
8383

8484
# retrive mapbox token if one is set; otherwise, throw error
@@ -96,12 +96,48 @@ mapbox_token <- function() {
9696
token
9797
}
9898

99+
is_subplot <- function(p) {
100+
isTRUE(p$x$subplot)
101+
}
102+
103+
supply_defaults <- function(p) {
104+
# no need to supply defaults for subplots
105+
if (is_subplot(p)) return(p)
106+
# supply trace anchor defaults
107+
anchors <- if (is_geo(p)) c("geo" = "geo") else if (is_mapbox(p)) c("subplot" = "mapbox") else c("xaxis" = "x", "yaxis" = "y")
108+
109+
p$x$data <- lapply(p$x$data, function(tr) {
110+
for (i in seq_along(anchors)) {
111+
nm <- names(anchors)[[i]]
112+
113+
tr[[nm]] <- unique(tr[[nm]]) %||% anchors[[i]]
114+
}
115+
tr
116+
})
117+
# supply domain defaults
118+
geoDomain <- list(x = c(0, 1), y = c(0, 1))
119+
if (is_geo(p) || is_mapbox(p)) {
120+
p$x$layout[grepl("^[x-y]axis", names(p$x$layout))] <- NULL
121+
p$x$layout[[p$x$layout$mapType]] <- modify_list(
122+
list(domain = geoDomain), p$x$layout[[p$x$layout$mapType]]
123+
)
124+
} else {
125+
for (axis in c("xaxis", "yaxis")) {
126+
p$x$layout[[axis]] <- modify_list(
127+
list(domain = c(0, 1)), p$x$layout[[axis]]
128+
)
129+
}
130+
}
131+
p
132+
}
133+
99134
# make sure plot attributes adhere to the plotly.js schema
100135
verify_attr_names <- function(p) {
101136
# some layout attributes (e.g., [x-y]axis can have trailing numbers)
102137
check_attrs(
103138
sub("[0-9]+$", "", names(p$x$layout)),
104-
c(names(Schema$layout$layoutAttributes), c("barmode", "bargap"))
139+
c(names(Schema$layout$layoutAttributes), c("barmode", "bargap", "mapType")),
140+
"layout"
105141
)
106142
for (tr in seq_along(p$x$data)) {
107143
thisTrace <- p$x$data[[tr]]
@@ -114,7 +150,7 @@ verify_attr_names <- function(p) {
114150
check_attrs <- function(proposedAttrs, validAttrs, type = "scatter") {
115151
illegalAttrs <- setdiff(proposedAttrs, validAttrs)
116152
if (length(illegalAttrs)) {
117-
warning("'", type, "' traces don't have these attributes: '",
153+
warning("'", type, "' objects don't have these attributes: '",
118154
paste(illegalAttrs, collapse = "', '"), "'\n",
119155
"Valid attributes include:\n'",
120156
paste(validAttrs, collapse = "', '"), "'\n",

0 commit comments

Comments
 (0)