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

Create plotly.js bundle map programmatically #2336

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Description: Create interactive web graphics from 'ggplot2' graphs and/or a cust
URL: https://plotly-r.com, https://github.com/plotly/plotly.R, https://plotly.com/r/
BugReports: https://github.com/plotly/plotly.R/issues
Depends:
R (>= 3.2.0),
R (>= 3.5),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this necessary?

Copy link
Contributor Author

@salim-b salim-b Jan 31, 2024

Choose a reason for hiding this comment

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

It's a requirement for R's latest serialization format (v3), cf. this line. We could stay on the v2 format if it's really important to support R < 3.5.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The line you're referring to is a build time, not run time dependency, so the version bump requirement isn't necessary

Copy link
Contributor Author

@salim-b salim-b Sep 3, 2024

Choose a reason for hiding this comment

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

Yeah, but it also becomes a runtime dependency once we ship pkg-internal data in v3 format (which we do with this PR). To cite the relevant doc:

Version 3 became the default from R 3.6.0 and can only be read by R versions 3.5.0 and higher.

Copy link
Collaborator

@cpsievert cpsievert Sep 3, 2024

Choose a reason for hiding this comment

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

Ahh, right, thanks for pointing that out. Could we just keep the current behavior? It doesn't seem worth it to me to break compatibility for relatively small R objects that only get loaded once

Copy link
Contributor Author

@salim-b salim-b Sep 3, 2024

Choose a reason for hiding this comment

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

Keeping the current behaviour is fine with me. But OTOH, R 3.5.0 was released more than 6 years ago, so I don't think we ought to care about supporting even more ancient versions.

I'll have a look at your other comments tomorrow. I'll revert to v2 serialization format then if you still think it's worth it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted to the v2 serialization format, see 93ef777.

salim-b marked this conversation as resolved.
Show resolved Hide resolved
ggplot2 (>= 3.0.0)
Imports:
tools,
Expand Down Expand Up @@ -77,7 +77,7 @@ Suggests:
reticulate,
rsvg
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Config/Needs/check:
Expand Down
62 changes: 3 additions & 59 deletions R/partial_bundles.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ verify_partial_bundle <- function(p) {

if (identical(bundleType, "auto")) {

# resolve an auto bundle by using the 1st bundle that supports all the types
# (ordering of bundleTraceMap is important!)
for (i in seq_along(bundleTraceMap)) {
# resolve an auto bundle by using the 1st bundle that supports all the types,
# except for the 'strict' bundle (ordering of bundleTraceMap is important!)
for (i in seq_along(bundleTraceMap[names(bundleTraceMap) != "strict"])) {
if (all(types %in% bundleTraceMap[[i]])) {
bundleType <- names(bundleTraceMap)[[i]]
break
Expand Down Expand Up @@ -158,59 +158,3 @@ plotlyjsBundleIDX <- function(p) {
plotlyjsBundle <- function(p) {
p$dependencies[[plotlyjsBundleIDX(p)]]
}

# TODO: create this object in inst/plotlyjs.R from the dist/README.md
bundleTraceMap <- list(
basic = c(
"scatter",
"bar",
"pie"
),
cartesian = c(
"scatter",
"bar",
"pie",
"box",
"heatmap",
"histogram",
"histogram2d",
"histogram2dcontour",
"contour",
"scatterternary",
"violin"
),
geo = c(
"scatter",
"scattergeo",
"choropleth"
),
gl3d = c(
"scatter",
"scatter3d",
"surface",
"mesh3d",
"cone"
),
gl2d = c(
"scatter",
"scattergl",
"splom",
"pointcloud",
"heatmapgl",
"contourgl",
"parcoords"
),
mapbox = c(
"scatter",
"scattermapbox"
),
finance = c(
"scatter",
"bar",
"pie",
"histogram",
"ohlc",
"candlestick",
"waterfall"
)
)
Binary file modified R/sysdata.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testthat/test-plotly-partial-bundles.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test_that("Throws an informative error if wrong bundle is specified", {

expect_error(
partial_bundle(p1, type = "basic"),
"The 'basic' bundle supports the following trace types: 'scatter', 'bar', 'pie'"
"The 'basic' bundle supports the following trace types: 'bar', 'pie', 'scatter'"
)
})

Expand Down
42 changes: 28 additions & 14 deletions tools/update_plotlyjs.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
library(httr)
library(rprojroot)

# get zip URL to latest plotly.js release
x <- RETRY(
x <- httr::RETRY(
verb = "GET",
url = 'https://api.github.com/repos/plotly/plotly.js/releases/latest',
times = 5,
terminate_on = c(400, 401, 403, 404),
terminate_on_success = TRUE
)
zip <- content(x)$zipball_url
zip <- httr::content(x)$zipball_url

# remember where to copy over assets
pkg_dir <- find_package_root_file()
lib_dir <- find_package_root_file("inst/htmlwidgets/lib/plotlyjs")
pkg_dir <- rprojroot::find_package_root_file()
lib_dir <- rprojroot::find_package_root_file("inst/htmlwidgets/lib/plotlyjs")
patches <- list.files(
find_package_root_file("tools/patches"),
rprojroot::find_package_root_file("tools/patches"),
full.names = TRUE
)

Expand All @@ -25,8 +22,8 @@ dir.create(tmpdir)

withr::with_dir(tmpdir, {
# download source
download.file(zip, "plotly-js.zip")
unzip("plotly-js.zip", exdir = "plotly-js")
utils::download.file(zip, "plotly-js.zip")
utils::unzip("plotly-js.zip", exdir = "plotly-js")

withr::with_dir(
dir("plotly-js", full.names = TRUE), {
Expand Down Expand Up @@ -65,10 +62,29 @@ withr::with_dir(tmpdir, {
file.path(lib_dir, "locales", sub("^plotly-locale-", "", basename(locales))),
overwrite = TRUE
)
# update plot schema
# update plot schema and (partial) bundles
Schema <- jsonlite::fromJSON(Sys.glob("dist/plot-schema.json"))
tmp_file <- tempfile(pattern = "plotly_constants_", fileext = ".js")
utils::download.file(
url = paste0("https://raw.githubusercontent.com/plotly/plotly.js/", basename(zip), "/tasks/util/constants.js"),
destfile = tmp_file,
quiet = TRUE,
mode = "wb"
)
bundleTraceMap <-
salim-b marked this conversation as resolved.
Show resolved Hide resolved
paste0(readLines(tmp_file), collapse = "\n") |>
stringr::str_extract(pattern = "(?<=var partialBundleTraces = )\\{[^}]+\\}") |>
yaml::read_yaml(text = _)

Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd be nice if this could also handle const as well as throw informative errors if the str_extract() comes up empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 74a4f4f.

withr::with_dir(
pkg_dir, usethis::use_data(Schema, overwrite = TRUE, internal = TRUE)
pkg_dir, usethis::use_data(
Schema,
bundleTraceMap,
internal = TRUE,
overwrite = TRUE,
compress = "xz",
version = 3L
)
)

# plotly.js used to bundle a typedarray polyfill to support older browsers,
Expand All @@ -81,7 +97,5 @@ withr::with_dir(tmpdir, {
#)

message("Update plotlyMainBundle()'s version with ", basename(zip))

})

})
Loading