Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}
# - {os: ubuntu-latest, r: 'oldrel-3'} # dependency issues with oldrel-3
# - {os: ubuntu-latest, r: 'oldrel-4'} # dependency issues with oldrel-4

env:
Expand Down Expand Up @@ -72,7 +72,8 @@ jobs:
- name: Install kaleido
if: matrix.config.visual_tests == true
run: |
Rscript -e 'library(reticulate); use_python(Sys.which("python")); py_install(c("kaleido", "plotly"))'
# We pin kaleido to v0.2.1 here since >=v1.0 doesn't appear to be correctly rendering some plots
Rscript -e 'library(reticulate); use_python(Sys.which("python")); py_install(c("kaleido==0.2.1", "plotly"))'

# Run test() before R CMD check since, for some reason, rcmdcheck::rcmdcheck() skips vdiffr tests
- name: Run Tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Rapp.history
*.RData
*.Rproj.user
*.DS_Store
.venv
node_modules/
build_site.R
revdep_email.R
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Suggests:
rsvg,
ggridges
LazyData: true
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Config/Needs/check:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ S3method(linewidth_or_size,default)
S3method(linewidth_or_size,element)
S3method(plotly_build,"NULL")
S3method(plotly_build,gg)
S3method(plotly_build,ggmatrix)
S3method(plotly_build,list)
S3method(plotly_build,plotly)
S3method(print,api)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# plotly (development version)

## Improvements

* `save_image()` now works with kaleido v1.0 and higher. (#2447)

## Bug fixes

* `plotly_build()` now works with `ggmatrix` objects (e.g., from `GGally::ggpairs()`). (#2447)

# plotly 4.11.0

## New features
Expand Down
63 changes: 61 additions & 2 deletions R/kaleido.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ kaleido <- function(...) {

call_env <- rlang::caller_env()

if (!reticulate::py_available()) {
if (!reticulate::py_available(TRUE)) {
rlang::abort(c("`{reticulate}` wasn't able to find a Python environment.",
i = "If you have an existing Python installation, use `reticulate::use_python()` to inform `{reticulate}` of it.",
i = "To have `{reticulate}` install Python for you, `reticulate::install_python()`."
Expand All @@ -97,6 +97,66 @@ kaleido <- function(...) {
}
)

res <- if (is.null(tryNULL(kaleido$scopes))) {
newKaleidoScope(kaleido)
} else {
legacyKaleidoScope(kaleido)
}

class(res) <- "kaleidoScope"
res
}

newKaleidoScope <- function(kaleido) {
list(
scopes = NULL,
transform = function(p, file, ..., width = NULL, height = NULL, scale = NULL) {
# Perform JSON conversion exactly how the R package would do it
fig_data <- plotly_build(p)$x[c("data", "layout", "config")]

# Inject mapbox token into layout.mapbox.accesstoken if available
# We use layout instead of config because Kaleido's parser preserves
# layout but drops config. This handles the case where users set
# MAPBOX_TOKEN env var but don't use plot_mapbox()
mapbox <- Sys.getenv("MAPBOX_TOKEN", NA)
if (!is.na(mapbox) && is.null(fig_data$layout$mapbox$accesstoken)) {
fig_data$layout$mapbox$accesstoken <- mapbox
}

fig <- to_JSON(fig_data)

# Write to JSON file
tmp_json <- tempfile(fileext = ".json")
on.exit(unlink(tmp_json))
writeLines(fig, tmp_json)

# Import it as a fig (dict)
load_json <- sprintf(
"import json; fig = json.load(open('%s'))",
tmp_json
)
reticulate::py_run_string(load_json)

# Gather figure-level options
opts <- list(
format = tools::file_ext(file),
width = reticulate::r_to_py(width),
height = reticulate::r_to_py(height),
scale = reticulate::r_to_py(scale)
)

# Pass the R plotly.js bundle path to Kaleido
kopts <- list(plotlyjs = plotlyMainBundlePath())

# Write the figure to a file using kaleido
kaleido$write_fig_sync(reticulate::py$fig, file, opts = opts, kopts = kopts)
},
shutdown = function() {}
)
}


legacyKaleidoScope <- function(kaleido) {
py <- reticulate::py
scope_name <- paste0("scope_", new_id())
py[[scope_name]] <- kaleido$scopes$plotly$PlotlyScope(
Expand Down Expand Up @@ -151,7 +211,6 @@ kaleido <- function(...) {
reticulate::py_run_string(paste("del", scope_name))
})

class(res) <- "kaleidoScope"
res
}

Expand Down
5 changes: 5 additions & 0 deletions R/plotly_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ plotly_build.gg <- function(p, registerFrames = TRUE) {
plotly_build(ggplotly(p))
}

#' @export
plotly_build.ggmatrix <- function(p, registerFrames = TRUE) {
plotly_build(ggplotly(p))
}

#' @export
plotly_build.plotly <- function(p, registerFrames = TRUE) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@
],
"layout": {
"margin": {
"t": 25.74124809741248,
"t": 23.305936073059364,
"r": 7.3059360730593621,
"b": 39.69558599695587,
"b": 37.260273972602747,
"l": 31.415525114155255
},
"plot_bgcolor": "rgba(235,235,235,1)",
Expand Down Expand Up @@ -239,7 +239,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -252,7 +252,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "y",
"title": {
Expand Down Expand Up @@ -301,7 +301,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -314,7 +314,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "x",
"title": {
Expand All @@ -340,6 +340,7 @@
},
"yref": "paper",
"xref": "paper",
"layer": "below",
"x0": 0,
"x1": 1,
"y0": 0,
Expand All @@ -350,7 +351,7 @@
"legend": {
"bgcolor": "rgba(255,255,255,1)",
"bordercolor": "transparent",
"borderwidth": 1.8897637795275593,
"borderwidth": 0,
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
Expand Down Expand Up @@ -445,9 +446,9 @@
},
{
"name": "crosstalk",
"version": "1.2.1",
"version": "1.2.2",
"src": {
"href": "crosstalk-1.2.1"
"href": "crosstalk-1.2.2"
},
"meta": null,
"script": "js/crosstalk.min.js",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@
],
"layout": {
"margin": {
"t": 25.74124809741248,
"t": 23.305936073059364,
"r": 7.3059360730593621,
"b": 39.69558599695587,
"b": 37.260273972602747,
"l": 31.415525114155255
},
"plot_bgcolor": "rgba(235,235,235,1)",
Expand Down Expand Up @@ -242,7 +242,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -255,7 +255,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "y",
"title": {
Expand Down Expand Up @@ -304,7 +304,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -317,7 +317,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "x",
"title": {
Expand All @@ -343,6 +343,7 @@
},
"yref": "paper",
"xref": "paper",
"layer": "below",
"x0": 0,
"x1": 1,
"y0": 0,
Expand All @@ -353,7 +354,7 @@
"legend": {
"bgcolor": "rgba(255,255,255,1)",
"bordercolor": "transparent",
"borderwidth": 1.8897637795275593,
"borderwidth": 0,
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
Expand Down Expand Up @@ -448,9 +449,9 @@
},
{
"name": "crosstalk",
"version": "1.2.1",
"version": "1.2.2",
"src": {
"href": "crosstalk-1.2.1"
"href": "crosstalk-1.2.2"
},
"meta": null,
"script": "js/crosstalk.min.js",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@
],
"layout": {
"margin": {
"t": 25.74124809741248,
"t": 23.305936073059364,
"r": 7.3059360730593621,
"b": 39.69558599695587,
"b": 37.260273972602747,
"l": 31.415525114155255
},
"plot_bgcolor": "rgba(235,235,235,1)",
Expand Down Expand Up @@ -246,7 +246,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -259,7 +259,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "y",
"title": {
Expand Down Expand Up @@ -308,7 +308,7 @@
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"tickwidth": 0,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
Expand All @@ -321,7 +321,7 @@
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176002,
"gridwidth": 0,
"zeroline": false,
"anchor": "x",
"title": {
Expand All @@ -347,6 +347,7 @@
},
"yref": "paper",
"xref": "paper",
"layer": "below",
"x0": 0,
"x1": 1,
"y0": 0,
Expand All @@ -357,7 +358,7 @@
"legend": {
"bgcolor": "rgba(255,255,255,1)",
"bordercolor": "transparent",
"borderwidth": 1.8897637795275593,
"borderwidth": 0,
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
Expand Down Expand Up @@ -452,9 +453,9 @@
},
{
"name": "crosstalk",
"version": "1.2.1",
"version": "1.2.2",
"src": {
"href": "crosstalk-1.2.1"
"href": "crosstalk-1.2.2"
},
"meta": null,
"script": "js/crosstalk.min.js",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading