Skip to content

Commit 665504a

Browse files
authored
Fixes for the development version of ggplot2 (> v3.5.2); migrate to shinytest2 (#2359)
* Use new complete_theme() function to get the complete set of theme elements * Refactor and reuse logic in tests * Fix kaleido install * Accept good snapshot changes * Disable windows oldrel * Close #2357. In some cases, decor has min/max instead of value. In that case, treat max like value * Accept new historgram binning changes * Make sure layout-specific shapes go underneath traces * Accept new snapshots; migrate to shinytest2 * Check-in snapshots
1 parent ed106db commit 665504a

File tree

87 files changed

+167
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+167
-134
lines changed

.github/workflows/R-CMD-check.yaml

+2-19
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ jobs:
2929
# vdiffr & shinytest only runs on mac r-release since the results aren't cross-platform
3030
- {os: macOS-latest, r: 'release', visual_tests: true, node: "14.x", shinytest: true}
3131
- {os: windows-latest, r: 'release'}
32-
- {os: windows-latest, r: '4.1'}
33-
- {os: windows-latest, r: '3.6'}
32+
# - {os: windows-latest, r: 'oldrel-1'} # pak is having issues
3433
- {os: ubuntu-latest, r: 'devel'}
3534
- {os: ubuntu-latest, r: 'release'}
3635
- {os: ubuntu-latest, r: 'oldrel-1'}
@@ -62,26 +61,10 @@ jobs:
6261
cache-version: 3
6362
needs: check
6463

65-
- name: Set up Python 3.8
66-
uses: actions/setup-python@v2
67-
with:
68-
python-version: 3.8
69-
7064
- name: Install kaleido
7165
if: matrix.config.visual_tests == true
7266
run: |
73-
sudo chown -R $UID $CONDA # https://github.com/nextstrain/conda/issues/5
74-
Rscript -e "reticulate::install_miniconda()"
75-
Rscript -e "reticulate::conda_install('r-reticulate', 'python-kaleido')"
76-
Rscript -e "reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')"
77-
Rscript -e "reticulate::use_miniconda('r-reticulate')"
78-
79-
- name: Install shinytest deps
80-
if: matrix.config.shinytest == true
81-
run: |
82-
Rscript -e 'shinytest::installDependencies()'
83-
R CMD install .
84-
shell: bash
67+
Rscript -e 'library(reticulate); use_python(install_python()); py_install(c("kaleido", "plotly"))'
8568
8669
# Run test() before R CMD check since, for some reason, rcmdcheck::rcmdcheck() skips vdiffr tests
8770
- name: Run Tests

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Suggests:
5757
testthat,
5858
knitr,
5959
shiny (>= 1.1.0),
60-
shinytest (>= 1.3.0),
60+
shinytest2,
6161
curl,
6262
rmarkdown,
6363
Cairo,

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* `ggplotly()` now supports the `{ggridges}` package. (#2314)
66

7+
## Improvements
8+
9+
* `ggplotly()` now works better with the development version of ggplot2 (> v3.4.4). (#2315)
10+
711
## Bug fixes
812

913
* Closed #2337: Creating a new `event_data()` handler no longer causes a spurious reactive update of existing `event_data()`s. (#2339)

R/ggplotly.R

+22-7
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,8 @@ gg2list <- function(p, width = NULL, height = NULL,
463463
assign(var, built_env[[var]], envir = envir)
464464
}
465465

466-
# initiate plotly.js layout with some plot-wide theming stuff
467-
theme <- ggfun("plot_theme")(plot)
468-
elements <- names(which(sapply(theme, inherits, "element")))
469-
for (i in elements) {
470-
theme[[i]] <- ggplot2::calc_element(i, theme)
471-
}
466+
theme <- calculated_theme_elements(plot)
467+
472468
# Translate plot wide theme elements to plotly.js layout
473469
pm <- unitConvert(theme$plot.margin, "pixels")
474470
gglayout <- list(
@@ -1154,6 +1150,23 @@ gg2list <- function(p, width = NULL, height = NULL,
11541150
# Due to the non-standard use of assign() in g2list() (above)
11551151
utils::globalVariables(c("groupDomains", "layers", "prestats_data", "scales", "sets"))
11561152

1153+
# Get the "complete" set of theme elements and their calculated values
1154+
calculated_theme_elements <- function(plot) {
1155+
if (is.function(asNamespace("ggplot2")$complete_theme)) {
1156+
theme <- ggplot2::complete_theme(plot$theme)
1157+
elements <- names(theme)
1158+
} else {
1159+
theme <- ggfun("plot_theme")(plot)
1160+
elements <- names(which(sapply(theme, inherits, "element")))
1161+
}
1162+
1163+
for (i in elements) {
1164+
theme[[i]] <- ggplot2::calc_element(i, theme)
1165+
}
1166+
1167+
theme
1168+
}
1169+
11571170

11581171
#-----------------------------------------------------------------------------
11591172
# ggplotly 'utility' functions
@@ -1384,7 +1397,8 @@ rect2shape <- function(rekt = ggplot2::element_rect()) {
13841397
linetype = lty2dash(rekt$linetype)
13851398
),
13861399
yref = "paper",
1387-
xref = "paper"
1400+
xref = "paper",
1401+
layer = "below"
13881402
)
13891403
}
13901404

@@ -1408,6 +1422,7 @@ gdef2trace <- function(gdef, theme, gglayout) {
14081422
# N.B. ggplot2 >v3.4.2 (specifically #4879) renamed bar to decor and also
14091423
# started returning normalized values for the key field
14101424
decor <- gdef$decor %||% gdef$bar
1425+
decor$value <- decor$value %||% decor$max
14111426
rng <- range(decor$value)
14121427
decor$value <- scales::rescale(decor$value, from = rng)
14131428
if (!"decor" %in% names(gdef)) {
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

inst/examples/shiny/event_data/tests/shinytest/mytest.R

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shinytest2::test_app()

inst/examples/shiny/event_data/tests/shinytest/mytest-expected/001.json renamed to inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/001.json

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
{
22
"input": {
3-
"plotType": "ggplotly"
3+
".clientValue-default-plotlyCrosstalkOpts": {
4+
"on": "plotly_click",
5+
"persistent": false,
6+
"dynamic": false,
7+
"selectize": false,
8+
"opacityDim": 0.2,
9+
"selected": {
10+
"opacity": 1
11+
},
12+
"debounce": 0,
13+
"color": [
14+
15+
]
16+
},
17+
"plotType": "ggplotly",
18+
"plotly_afterplot-A": "\"plot\""
419
},
520
"output": {
621
"brushed": "[1] \"Brush extents appear here (double-click to clear)\"",
Loading

inst/examples/shiny/event_data/tests/shinytest/mytest-expected/002.json renamed to inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/002.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
]
1616
},
17+
"plotType": "ggplotly",
1718
"plotly_afterplot-A": "\"plot\"",
1819
"plotly_click-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
1920
"plotly_hover-A": null,
20-
"plotly_relayout-A": "{\"width\":962,\"height\":400}",
21-
"plotType": "ggplotly"
21+
"plotly_relayout-A": "{\"width\":962,\"height\":400}"
2222
},
2323
"output": {
2424
"brushed": "[1] \"Brush extents appear here (double-click to clear)\"",
Loading

inst/examples/shiny/event_data/tests/shinytest/mytest-expected/003.json renamed to inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/003.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
]
1616
},
17+
"plotType": "ggplotly",
1718
"plotly_afterplot-A": "\"plot\"",
1819
"plotly_brushed-A": "{\"x\":[23.95978500551268,25.98332414553473],\"y\":[3.0020072289156627,3.5073743975903615]}",
1920
"plotly_brushing-A": "{\"x\":[23.95978500551268,25.98332414553473],\"y\":[3.0020072289156627,3.5073743975903615]}",
2021
"plotly_click-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
2122
"plotly_hover-A": null,
2223
"plotly_relayout-A": "{\"width\":962,\"height\":400}",
2324
"plotly_selected-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
24-
"plotly_selecting-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]",
25-
"plotType": "ggplotly"
25+
"plotly_selecting-A": "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]"
2626
},
2727
"output": {
2828
"brushed": "$x\n[1] 23.95979 25.98332\n\n$y\n[1] 3.002007 3.507374",
Loading

inst/examples/shiny/event_data/tests/shinytest/mytest-expected/004.json renamed to inst/examples/shiny/event_data/tests/testthat/_snaps/shinytest2/004.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
]
1616
},
17+
"plotType": "ggplotly",
1718
"plotly_afterplot-A": "\"plot\"",
1819
"plotly_brushed-A": null,
1920
"plotly_brushing-A": null,
@@ -22,8 +23,7 @@
2223
"plotly_hover-A": null,
2324
"plotly_relayout-A": "{\"width\":962,\"height\":400}",
2425
"plotly_selected-A": null,
25-
"plotly_selecting-A": null,
26-
"plotType": "ggplotly"
26+
"plotly_selecting-A": null
2727
},
2828
"output": {
2929
"brushed": "[1] \"Brush extents appear here (double-click to clear)\"",
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Load application support files into testing environment
2+
shinytest2::load_app_env()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
library(shinytest2)
2+
app <- AppDriver$new(
3+
"../../", view = interactive(),
4+
options = list(display.mode = "normal")
5+
)
6+
7+
test_that("Plotly input values in Shiny", {
8+
app$expect_values()
9+
app$set_inputs(`plotly_hover-A` = "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]", allow_no_input_binding_ = TRUE)
10+
app$set_inputs(`plotly_click-A` = "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]", allow_no_input_binding_ = TRUE)
11+
app$set_inputs(`plotly_hover-A` = character(0), allow_no_input_binding_ = TRUE)
12+
Sys.sleep(1)
13+
app$expect_values()
14+
app$set_inputs(`plotly_brushing-A` = "{\"x\":[25.726819184123485,25.98332414553473],\"y\":[1.3174499999999998,5.61955]}", allow_no_input_binding_ = TRUE)
15+
app$set_inputs(`plotly_selecting-A` = "[]", allow_no_input_binding_ = TRUE)
16+
app$set_inputs(`plotly_brushing-A` = "{\"x\":[24.64379823594267,25.98332414553473],\"y\":[3.2093373493975905,3.5073743975903615]}", allow_no_input_binding_ = TRUE)
17+
app$set_inputs(`plotly_brushing-A` = "{\"x\":[24.045286659316428,25.98332414553473],\"y\":[3.040881626506024,3.5073743975903615]}", allow_no_input_binding_ = TRUE)
18+
app$set_inputs(`plotly_selecting-A` = "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]", allow_no_input_binding_ = TRUE)
19+
app$set_inputs(`plotly_brushing-A` = "{\"x\":[23.95978500551268,25.98332414553473],\"y\":[3.0020072289156627,3.5073743975903615]}", allow_no_input_binding_ = TRUE)
20+
app$set_inputs(`plotly_selected-A` = "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]", allow_no_input_binding_ = TRUE)
21+
app$set_inputs(`plotly_brushed-A` = "{\"x\":[23.95978500551268,25.98332414553473],\"y\":[3.0020072289156627,3.5073743975903615]}", allow_no_input_binding_ = TRUE)
22+
app$set_inputs(`plotly_hover-A` = "[{\"curveNumber\":0,\"pointNumber\":7,\"x\":24.4,\"y\":3.19,\"customdata\":\"Merc 240D\"}]", allow_no_input_binding_ = TRUE)
23+
app$set_inputs(`plotly_hover-A` = character(0), allow_no_input_binding_ = TRUE)
24+
app$expect_values()
25+
app$set_inputs(`plotly_selected-A` = character(0), allow_no_input_binding_ = TRUE)
26+
app$set_inputs(`plotly_brushed-A` = character(0), allow_no_input_binding_ = TRUE)
27+
#app$set_inputs(`plotly_selected-A` = character(0), allow_no_input_binding_ = TRUE)
28+
app$set_inputs(`plotly_selecting-A` = character(0), allow_no_input_binding_ = TRUE)
29+
#app$set_inputs(`plotly_brushed-A` = character(0), allow_no_input_binding_ = TRUE)
30+
app$set_inputs(`plotly_brushing-A` = character(0), allow_no_input_binding_ = TRUE)
31+
app$set_inputs(`plotly_click-A` = character(0), allow_no_input_binding_ = TRUE)
32+
app$set_inputs(`plotly_deselect-A` = "\"plot\"", allow_no_input_binding_ = TRUE)
33+
app$expect_values()
34+
})

tests/testthat/_snaps/ggplot-contour/raster-contour-binned.svg

+1
Loading

tests/testthat/_snaps/ggplot-density/density-color.svg

+1-1
Loading

tests/testthat/_snaps/ggplot-density/density-fill.svg

+1-1
Loading

0 commit comments

Comments
 (0)