Skip to content
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

[pull] master from ropensci:master #2

Merged
merged 115 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
7185252
New version of scales no longer 'spans the gamut' of a qualitative co…
cpsievert Dec 3, 2018
eb2caca
reduce Reduce() for merging lists
alyst Mar 18, 2019
db622ad
ensure_one(): reduce noise if attrs are NULL
alyst Mar 18, 2019
0b74e8d
ggplotly: fix Xaxis anchor if the last row incomplete
alyst Mar 18, 2019
414fbe1
stylistic changes
cpsievert Apr 3, 2019
8a8878d
validate new visual baselines
cpsievert Apr 3, 2019
51a8099
Merge branch 'master' into avoid_reduce
cpsievert Apr 4, 2019
8a5b8c3
merge conflicts
cpsievert Apr 4, 2019
51c7437
Merge branch 'master' into fix_wrap_axes
cpsievert Apr 8, 2019
3c85052
Doesn't seem like we need to handle yaxis being a factor
cpsievert Apr 8, 2019
8becdb9
update shinytest baselines
cpsievert Jul 10, 2019
5be39f7
ugh maintaining shinytest tests for htmlwidgets is a real pain
cpsievert Jul 10, 2019
6f151a7
Merge pull request #1557 from ropensci/v1.48
cpsievert Jul 10, 2019
2a439bc
update sf baselines
cpsievert Jul 13, 2019
a0fa68f
go back to using ggplotly() to prepare the widget for shiny rendering
cpsievert Jul 13, 2019
237f89d
Generation of non-intercept data in hline/vline should respect coord_…
cpsievert Jul 12, 2019
3bdb921
Merge pull request #1498 from alyst/avoid_reduce
cpsievert Jul 15, 2019
ddb8b30
merge with master
cpsievert Jul 15, 2019
3f3d875
Merge pull request #1501 from alyst/fix_wrap_axes
cpsievert Jul 15, 2019
ecf3b39
update news; fix typo
cpsievert Jul 15, 2019
5178c5d
Account for new changes in ggplot2's internal API, fixes #1561
cpsievert Jul 11, 2019
7efd029
include braces
cpsievert Jul 12, 2019
b6de355
gsub
cpsievert Jul 13, 2019
74b9168
moar braces
cpsievert Jul 16, 2019
9aaffa2
new sf baselines
cpsievert Jul 16, 2019
9a9876f
Merge pull request #1572 from ropensci/ggplot2-dev
cpsievert Jul 16, 2019
49da4f0
Merge pull request #1576 from ropensci/flip-lines
cpsievert Jul 17, 2019
a689263
update news
cpsievert Jul 17, 2019
40b1a1c
upgrade to plotly.js v1.49.0
cpsievert Jul 24, 2019
13ecba2
upgrade to plotly.js v1.49.2
cpsievert Aug 13, 2019
35c2bb3
upgrade to plotly.js v1.49.4
cpsievert Aug 26, 2019
3572a00
update baseline
cpsievert Aug 26, 2019
eeb33a1
update shinytest baseline
cpsievert Aug 27, 2019
d11bb5a
Merge pull request #1585 from ropensci/v1.49
cpsievert Aug 27, 2019
e07f80c
make a deep copy of x.layout.width/x.layout.height for use in the res…
cpsievert Aug 28, 2019
dd474e2
Wrap user-supplied expression in evalq(), closes #1528
cpsievert May 24, 2019
af1c770
Use shiny::installExprFunction to register debug hooks
cpsievert Aug 26, 2019
3867737
Asynchronously register plot events
cpsievert Aug 29, 2019
d416cea
Merge pull request #1539 from ropensci/renderWidgetPrep
cpsievert Aug 30, 2019
2856731
Revert "Merge pull request #1539 from ropensci/renderWidgetPrep"
cpsievert Aug 30, 2019
a0cd2f2
no need to be making deep copies
cpsievert Aug 30, 2019
8bc36b9
Improvements to algorithm for attaching key attribute for shiny's eve…
cpsievert Sep 3, 2019
8056cc5
When pointNumber is a constant, relay it as such
cpsievert Sep 3, 2019
81caeeb
handle the 3 cases separately
cpsievert Sep 3, 2019
30e3997
use idx instead of i
cpsievert Sep 3, 2019
17466a3
Merge pull request #1611 from ropensci/shiny-event-data-key
cpsievert Sep 3, 2019
972a4cb
update news
cpsievert Sep 3, 2019
6844d74
Call setInputValue only if it's defined, closes #1624
cpsievert Sep 23, 2019
08d402c
Merge pull request #1578 from ropensci/prepareWidget
cpsievert Oct 8, 2019
7fbb486
Revert "Revert "Merge pull request #1539 from ropensci/renderWidgetPr…
cpsievert Aug 30, 2019
1a714b8
pass along a quoted expr that calls func()
cpsievert Aug 30, 2019
3fd7e39
Merge pull request #1609 from ropensci/renderWidgetPrep2
cpsievert Oct 8, 2019
6f1925a
Merge pull request #1575 from ropensci/resize
cpsievert Oct 8, 2019
5c77075
Revert "go back to using ggplotly() to prepare the widget for shiny r…
cpsievert Oct 8, 2019
6ecca60
update news
cpsievert Oct 8, 2019
dcd4219
verify_webgl() shouldn't warn about types that are already gl, closes…
cpsievert Oct 8, 2019
4c0c7d8
Merge pull request #1625 from ropensci/static-shiny-render
cpsievert Oct 8, 2019
6d90509
fix test
cpsievert Oct 8, 2019
d41a675
Do not create structure with NULL to remove warnings
Oct 15, 2019
c299c5f
Fix management of environments in renderPlotly(), fixes #1639
cpsievert Oct 16, 2019
599b851
Add test to prove #1299 fix
Oct 19, 2019
5e1feb3
Merge pull request #1638 from wfjvdham/check_NA_for_lines
cpsievert Oct 21, 2019
c889fa2
proposed change to avoid peeking at 'grid' unit internals
pmur002 Oct 22, 2019
c708f76
improve previous commit
pmur002 Oct 23, 2019
7987280
Merge pull request #1646 from pmur002/master
cpsievert Oct 23, 2019
cdfdc7c
refactor Paul's unit type commits from #1646
cpsievert Oct 23, 2019
a3a1941
Some CRAN builds don't have pandoc
cpsievert Nov 2, 2019
f9b623e
Merge pull request #1421 from ropensci/scales
cpsievert Nov 6, 2019
4d83031
Merge branch 'master' of github.com:ropensci/plotly
cpsievert Nov 6, 2019
8108b20
plot_mapbox() shouldn't force a scattermapbox trace type, closes #1608
cpsievert Nov 6, 2019
daf0c35
fix R CMD check notes
cpsievert Nov 6, 2019
3cbccfc
r-devel apparently doesn't yet have grid::unitType()
cpsievert Nov 6, 2019
6553f8b
remove outdated travis config
cpsievert Nov 6, 2019
290c8ef
Validate changes visual baselines due to changes in R3.6's new RNG me…
cpsievert Nov 6, 2019
bcae42b
Add support for new plotly_sunburstclick event, closes #1648
cpsievert Nov 6, 2019
683aa1b
validate new shinytest baselines
cpsievert Nov 6, 2019
ff27ca5
update news
cpsievert Nov 6, 2019
7b32e7e
fix silly mistake from 3cbccfc508ed239e99739cc816ebf9067e019674
cpsievert Nov 7, 2019
ba86b9d
document
cpsievert Nov 7, 2019
71da78c
test expectation should account for changes in the default color pale…
cpsievert Nov 7, 2019
2bbb5dc
improve the aforementioned test example and add a visual test
cpsievert Nov 7, 2019
27db11e
Ignore calculated aesthetics that match specified aesthestics
cpsievert Oct 2, 2019
bfafb5e
fix broken link raster2uri and improve the description
cpsievert Nov 7, 2019
bf08221
cran hyperlinks must be https
cpsievert Nov 7, 2019
b5cbae3
fix roxygen warning
cpsievert Nov 7, 2019
006b69a
In contrast to attr(x, unit), the new grid::unitType() function alway…
cpsievert Nov 7, 2019
9bcf90a
bump to release version; submit to CRAN
cpsievert Nov 7, 2019
c037c68
Respect sf's plot12 graticule attribute when building axis ticks, fix…
cpsievert Dec 26, 2019
63eaf39
Remove missing values in ticktext/tickvals
cpsievert Jan 6, 2020
e85ee24
link to comment
cpsievert Jan 6, 2020
9f6fa20
remove outdated/bad test expectation...the visual baseline covers it
cpsievert Jan 6, 2020
669db9c
Merge pull request #1650 from ropensci/ggplotly-calculated-tooltip
cpsievert Jan 7, 2020
c47c1f9
Merge pull request #1679 from ropensci/ggplotly-missing-ticks
cpsievert Jan 7, 2020
7d2ea6d
Don't assume sf geometry is always accessible via [['geometry']], fix…
cpsievert Feb 4, 2020
c3fe34b
validate baselines
cpsievert Feb 5, 2020
eb0a460
update news
cpsievert Feb 5, 2020
af255a7
update to plotly.js 1.52.2
cpsievert Feb 4, 2020
91a0826
new baselines
cpsievert Feb 5, 2020
397e289
Add image and treemap visual tests. Also, ensure treemap's stroke ref…
cpsievert Feb 5, 2020
7ffa87f
Add Object.setPrototypeOf polyfill for shinytest and update shinytest…
cpsievert Feb 6, 2020
cabc57d
Add add_image() for easier rendering of image traces via raster objects
cpsievert Feb 6, 2020
7f7a60d
Merge pull request #1690 from ropensci/sf-geometry
cpsievert Feb 6, 2020
cead5d9
Merge pull request #1695 from ropensci/v1.52
cpsievert Feb 6, 2020
d6a95cf
Bump version, update news, document, add unit test for add_image()
cpsievert Feb 6, 2020
37a2708
roxygen escapes % now
cpsievert Feb 6, 2020
65493ad
Only call locale_dependency() for locales not included with standard …
cpsievert Feb 6, 2020
a63b048
CRAN submission
cpsievert Feb 12, 2020
1e243ba
plot_mapbox() should default to scattermapbox unless z is present, fi…
cpsievert Feb 24, 2020
f8af657
Merge pull request #1674 from ropensci/sf-plot12-ticks
cpsievert Mar 4, 2020
684c779
Merge pull request #1635 from ropensci/verify-webgl
cpsievert Mar 4, 2020
9e427fc
add visual test for #1674
cpsievert Mar 4, 2020
273c4ed
update poor test expectations that assume stringsAsFactors defaults t…
cpsievert Mar 4, 2020
0906548
bump version; update news
cpsievert Mar 4, 2020
25b28c9
update visual tests
cpsievert Mar 4, 2020
33fc723
remove rnaturalearth dependency in tests
cpsievert Mar 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,3 @@ before_install:
before_script:
- if [[ "$TRAVIS_R_VERSION_STRING" == "release" ]]; then docker run -e GITHUB_PAT=$GITHUB_PAT -e MAPBOX_TOKEN=$MAPBOX_TOKEN -e VMODE="ci" -v $(pwd):/home/plotly --privileged cpsievert/plotly-orca; fi

# work around temporary travis + R 3.5 bug
r_packages: devtools

# temporary: needed for plotly input testing in shiny
r_github_packages:
- rstudio/shinytest
- r-lib/remotes
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: plotly
Title: Create Interactive Web Graphics via 'plotly.js'
Version: 4.9.0.9000
Version: 4.9.2.1
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
email = "[email protected]", comment = c(ORCID = "0000-0002-4958-2844")),
person("Chris", "Parmer", role = "aut",
Expand Down Expand Up @@ -72,6 +72,6 @@ Suggests:
plotlyGeoAssets,
forcats
LazyData: true
RoxygenNote: 6.1.1
RoxygenNote: 7.0.2
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export(add_heatmap)
export(add_histogram)
export(add_histogram2d)
export(add_histogram2dcontour)
export(add_image)
export(add_lines)
export(add_markers)
export(add_mesh)
Expand Down
40 changes: 38 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
# 4.9.0.9000
# 4.9.2.1

This is minor patch release with a few minor bug fixes and updates test expectations in anticipation of new R 4.0 defaults.

## BUG FIXES

* `plot_mapbox()` now correctly defaults to a scattermapbox trace (unless z is present, then it defaults to choroplethmapbox) (#1702).
* `ggplotly()` now correctly resolves overlapping axis tick text in `coord_sf()` (#1673).
* A false-positive warning is no longer thrown when attempting to cast `toWebGL()` (#1569).

# 4.9.2

## Changes to plotly.js

* This version of the R package upgrades the version of the underlying plotly.js library from v1.49.4 to v1.52.2. This includes many bug fixes, improvements, as well as 2 new trace types: `treemap` and `image`. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes.

## IMPROVEMENTS

* The `add_image()` function was added to make it easier to create image traces via `raster` objects.

## BUG FIXES

* `add_sf()`/`geom_sf()` now correctly handle geometry columns that are named something other than `"geometry"` (#1659).
* Specifying an english locale no longer results in error (#1686).

# 4.9.1

## Changes to plotly.js

* This version of the R package upgrades the version of the underlying plotly.js library from v1.46.1 to v1.48.3. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes.
* This version of the R package upgrades the version of the underlying plotly.js library from v1.46.1 to v1.49.4. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes.

## IMPROVEMENTS

* `event_data()` gains support for the `plotly_sunburstclick` event (#1648)

## BUG FIXES

* Fixed an issue with correctly capturing the return value of user-expressions to `renderPlotly()` (#1528).
* Fixed a resizing issue where graphs could be incorrectly resized to their initial size in some cases (#1553).
* `ggplotly()` now positions the x-axis in the last column of a `facet_wrap()` properly (#1501).
* `ggplotly()` now handles `geom_hline()`/`geom_vline()` correctly in conjunction with `coord_flip()` (#1519).
* `event_data()` now correctly relays the `key` attribute for statistical traces (#1610).

# 4.9.0

Expand Down
39 changes: 38 additions & 1 deletion R/add.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ add_data <- function(p, data = NULL) {
#' @inheritParams plot_ly
#' @param p a plotly object
#' @param inherit inherit attributes from [plot_ly()]?
#' @param z a numeric matrix
#' @param z a numeric matrix (unless [add_image()], which wants a raster object, see [as.raster()]).
#' @param x the x variable.
#' @param y the y variable.
#' @param text textual labels.
Expand Down Expand Up @@ -393,6 +393,43 @@ add_ribbons <- function(p, x = NULL, ymin = NULL, ymax = NULL, ...,
)
}

#' @inheritParams add_trace
#' @rdname add_trace
#' @param colormodel Sets the colormodel for image traces if `z` is not a raster object.
#' If `z` is a raster object (see [as.raster()]), the `'rgba'` colormodel is always used.
#' @export
add_image <- function(p, z = NULL, colormodel = NULL, ..., data = NULL, inherit = TRUE) {

if (inherit) {
z <- z %||% p$x$attrs[[1]][["z"]]
colormodel <- colormodel %||% p$x$attrs[[1]][["colormodel"]]
}

if (inherits(z, "raster")) {
cols <- col2rgb(z, alpha = TRUE)
dims <- c(dim(z), 4)
z <- array(numeric(prod(dims)), dims)
matrix_ <- function(x) {
matrix(x, byrow = TRUE, nrow = dims[1], ncol = dims[2])
}
z[,,1] <- matrix_(cols["red",])
z[,,2] <- matrix_(cols["green",])
z[,,3] <- matrix_(cols["blue",])
z[,,4] <- matrix_(cols["alpha",])

# Throw if we detect another colormodel
if (!identical(colormodel %||% "rgba", "rgba")) {
warning("Passing a raster object to z requires rgba colormodel")
}
colormodel <- "rgba"
}

add_trace(
p, z = z, colormodel = colormodel, ...,
data = data, type = "image"
)
}

#' @inheritParams add_trace
#' @rdname add_trace
#' @param r For polar chart only. Sets the radial coordinates.
Expand Down
2 changes: 1 addition & 1 deletion R/embed.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Embed a plot as an iframe into a Jupyter Notebook
#' @param x a plotly object
#' @param width attribute of the iframe. If `NULL`, the width in
#' `plot_ly` is used. If that is also `NULL`, '100\%' is the default.
#' `plot_ly` is used. If that is also `NULL`, '100%' is the default.
#' @param height attribute of the iframe. If `NULL`, the height in
#' `plot_ly` is used. If that is also `NULL`, '400px' is the default.
#' @param file deprecated.
Expand Down
121 changes: 75 additions & 46 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,37 +465,51 @@ gg2list <- function(p, width = NULL, height = NULL,

# panel -> plotly.js axis/anchor info
# (assume a grid layout by default)
layout$layout$xaxis <- layout$layout$COL
layout$layout$yaxis <- layout$layout$ROW
layout$layout$xanchor <- nRows
layout$layout$yanchor <- 1
layout$layout <- dplyr::mutate(
layout$layout,
xaxis = COL,
yaxis = ROW,
xanchor = nRows,
yanchor = 1L
)
if (inherits(plot$facet, "FacetWrap")) {
if (plot$facet$params$free$x) {
layout$layout$xaxis <- layout$layout$PANEL
layout$layout$xanchor <- layout$layout$ROW
}
if (plot$facet$params$free$y) {
layout$layout$yaxis <- layout$layout$PANEL
layout$layout$yanchor <- layout$layout$COL
layout$layout$xanchor <- nPanels
}
if (plot$facet$params$free$x && plot$facet$params$free$y) {
layout$layout$xaxis <- layout$layout$PANEL
layout$layout$yaxis <- layout$layout$PANEL
layout$layout$xanchor <- layout$layout$PANEL
layout$layout$yanchor <- layout$layout$PANEL
layout$layout <- dplyr::mutate(
layout$layout,
xaxis = PANEL,
yaxis = PANEL,
xanchor = PANEL,
yanchor = PANEL
)
} else if (plot$facet$params$free$x) {
layout$layout <- dplyr::mutate(
layout$layout,
xaxis = PANEL,
xanchor = ROW
)
} else if (plot$facet$params$free$y) {
layout$layout <- dplyr::mutate(
layout$layout,
yaxis = PANEL,
yanchor = COL
)
}
# anchor X axis to the lowest plot in its column
layout$layout <- dplyr::group_by_(layout$layout, "xaxis")
layout$layout <- dplyr::mutate(layout$layout, xanchor = max(as.integer(yaxis)))
}
layout$layout <- as.data.frame(layout$layout)

# format the axis/anchor to a format plotly.js respects
layout$layout$xaxis <- paste0("xaxis", sub("^1$", "", layout$layout$xaxis))
layout$layout$yaxis <- paste0("yaxis", sub("^1$", "", layout$layout$yaxis))
layout$layout$xanchor <- paste0("y", sub("^1$", "", layout$layout$xanchor))
layout$layout$yanchor <- paste0("x", sub("^1$", "", layout$layout$yanchor))
# for some layers2traces computations, we need the range of each panel
layout$layout$x_min <- sapply(layout$panel_params, function(z) min(z$x.range %||% z$x_range))
layout$layout$x_max <- sapply(layout$panel_params, function(z) max(z$x.range %||% z$x_range))
layout$layout$y_min <- sapply(layout$panel_params, function(z) min(z$y.range %||% z$y_range))
layout$layout$y_max <- sapply(layout$panel_params, function(z) max(z$y.range %||% z$y_range))
layout$layout$x_min <- sapply(layout$panel_params, function(z) { min(z[["x"]]$dimension %()% z$x.range %||% z$x_range) })
layout$layout$x_max <- sapply(layout$panel_params, function(z) { max(z[["x"]]$dimension %()% z$x.range %||% z$x_range) })
layout$layout$y_min <- sapply(layout$panel_params, function(z) { min(z[["y"]]$dimension %()% z$y.range %||% z$y_range) })
layout$layout$y_max <- sapply(layout$panel_params, function(z) { max(z[["y"]]$dimension %()% z$y.range %||% z$y_range) })

# layers -> plotly.js traces
plot$tooltip <- tooltip
Expand Down Expand Up @@ -552,7 +566,7 @@ gg2list <- function(p, width = NULL, height = NULL,
)
# allocate enough space for the _longest_ text label
axisTextX <- theme[["axis.text.x"]] %||% theme[["axis.text"]]
labz <- unlist(lapply(layout$panel_params, "[[", "x.labels"))
labz <- unlist(lapply(layout$panel_params, function(pp) { pp[["x"]]$get_labels %()% pp$x.labels }))
lab <- labz[which.max(nchar(labz))]
panelMarginY <- panelMarginY + axisTicksX +
bbox(lab, axisTextX$angle, unitConvert(axisTextX, "npc", "height"))[["height"]]
Expand All @@ -564,7 +578,7 @@ gg2list <- function(p, width = NULL, height = NULL,
)
# allocate enough space for the _longest_ text label
axisTextY <- theme[["axis.text.y"]] %||% theme[["axis.text"]]
labz <- unlist(lapply(layout$panel_params, "[[", "y.labels"))
labz <- unlist(lapply(layout$panel_params, function(pp) { pp[["y"]]$get_labels %()% pp$y.labels }))
lab <- labz[which.max(nchar(labz))]
panelMarginX <- panelMarginX + axisTicksY +
bbox(lab, axisTextY$angle, unitConvert(axisTextY, "npc", "width"))[["width"]]
Expand Down Expand Up @@ -598,10 +612,19 @@ gg2list <- function(p, width = NULL, height = NULL,
if ("CoordSf" %in% class(p$coordinates)) {
# see CoordSf$render_axis_v
direction <- if (xy == "x") "E" else "N"
idx <- rng$graticule$type == direction & !is.na(rng$graticule$degree_label)
idx <- rng$graticule$type == direction &
!is.na(rng$graticule$degree_label) &
# Respect the logical 'plot12' column which sf constructs for
# determining which tick labels should be drawn
# https://github.com/r-spatial/sf/blob/b49d37/R/graticule.R#L199
# https://github.com/r-spatial/sf/blob/52a8351/R/plot.R#L580
(rng$graticule$plot12 %||% TRUE)
tickData <- rng$graticule[idx, ]
# TODO: how to convert a language object to unicode character string?
rng[[paste0(xy, ".labels")]] <- as.character(tickData[["degree_label"]])
rng[[paste0(xy, ".labels")]] <- sub(
"\\*\\s+degree[ ]?[\\*]?", "&#176;",
gsub("\"", "", tickData[["degree_label"]])
)
rng[[paste0(xy, ".major")]] <- tickData[[paste0(xy, "_start")]]

# If it doesn't already exist (for this panel),
Expand Down Expand Up @@ -636,14 +659,7 @@ gg2list <- function(p, width = NULL, height = NULL,
tickExists <- with(rng$graticule, sapply(degree_label, is.language))
if (sum(tickExists) == 0) {
theme$axis.ticks.length <- 0
} else{
# convert the special *degree expression in plotmath to HTML entity
# TODO: can this be done more generally for all ?
rng[[paste0(xy, ".labels")]] <- sub(
"\\*\\s+degree[ ]?[\\*]?", "&#176;", rng[[paste0(xy, ".labels")]]
)
}

}

# stuff like layout$panel_params is already flipped, but scales aren't
Expand Down Expand Up @@ -673,16 +689,23 @@ gg2list <- function(p, width = NULL, height = NULL,
isDiscrete <- identical(sc$scale_name, "position_d")
isDiscreteType <- isDynamic && isDiscrete

ticktext <- rng[[xy]]$get_labels %()% rng[[paste0(xy, ".labels")]]
tickvals <- rng[[xy]]$break_positions %()% rng[[paste0(xy, ".major")]]

# https://github.com/tidyverse/ggplot2/pull/3566#issuecomment-565085809
ticktext <- ticktext[!is.na(ticktext)]
tickvals <- tickvals[!is.na(tickvals)]

axisObj <- list(
# TODO: log type?
type = if (isDateType) "date" else if (isDiscreteType) "category" else "linear",
autorange = isDynamic,
range = rng[[paste0(xy, ".range")]] %||% rng[[paste0(xy, "_range")]],
tickmode = if (isDynamic) "auto" else "array",
ticktext = rng[[paste0(xy, ".labels")]],
tickvals = rng[[paste0(xy, ".major")]],
ticktext = ticktext,
tickvals = tickvals,
categoryorder = "array",
categoryarray = rng[[paste0(xy, ".labels")]],
categoryarray = ticktext,
nticks = nrow(rng),
ticks = if (is_blank(axisTicks)) "" else "outside",
tickcolor = toRGB(axisTicks$colour),
Expand Down Expand Up @@ -1124,22 +1147,28 @@ unitConvert <- function(u, to = c("npc", "pixels"), type = c("x", "y", "height",
# from R, but it seems 96 is a reasonable assumption.
mm2pixels <- function(u) {
u <- verifyUnit(u)
if (attr(u, "unit") != "mm") {
stop("Unit must be in millimeters")
if (any(getUnitType(u) != "mm")) {
stop("All units must be in millimeters")
}
(as.numeric(u) * 96) / 25.4
}

verifyUnit <- function(u) {
# the default unit in ggplot2 is millimeters (unless it's element_text())
if (is.null(attr(u, "unit"))) {
u <- if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
}
if (grid::is.unit(u)) return(u)

## the default unit in ggplot2 is millimeters (unless it's element_text())
if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
}
u
}

# Use public API for getting the unit's type, if available
# https://github.com/ropensci/plotly/pull/1646#issue-331268260
getUnitType <- function(u) {
tryNULL(get("unitType", envir = asNamespace("grid"))(u)) %||%
attr(u, "unit")
}

# detect a blank theme element
Expand Down
9 changes: 6 additions & 3 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,18 @@ plotly_empty <- function(...) {
}


#' Convert a raster object to a data URI
#' Encode a raster object as a data URI
#'
#' Convenient embedding images via [layout()]
#' \href{images}{https://plot.ly/r/reference/#layout-images}.
#' Encode a raster object as a data URI, which is suitable for
#' use with `layout()` \href{https://plot.ly/r/reference/#layout-images}{images}.
#' This is especially convenient for embedding raster images on a plot in
#' a self-contained fashion (i.e., so they don't depend on external URL links).
#'
#' @param r an object coercable to a raster object via [as.raster()]
#' @param ... arguments passed onto [as.raster()].
#' @author Carson Sievert
#' @export
#' @references <https://plotly-r.com/embedding-images.html>
#' @examples
#'
#' # a red gradient (from ?as.raster)
Expand Down
Loading