Skip to content

Commit 98dd88e

Browse files
JustinCCYapchschan
authored andcommitted
VIS-992: add test, use rhtmlBuildUtils (#15)
* Initial commit * Remove prepush * Final line for plotly.yml * Re-add lib * Refactor * Create test * Specify rhtmlBuildUtils version * Remove preinstall script * Delete package-lock.json * Update documentation * Update documentation and remove library * Restore files * Restore file * Update build files
1 parent b749dfe commit 98dd88e

File tree

9 files changed

+1023
-115
lines changed

9 files changed

+1023
-115
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "12"
4+
sudo: required
5+
dist: xenial
6+
before_install:
7+
- sudo apt-get update
8+
script:
9+
- jest

README.md

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1 @@
1-
2-
<!-- README.md is generated from README.Rmd. Please edit that file -->
3-
4-
<img src="man/figures/plotly.png" width="200" />
5-
6-
<!-- badges: start -->
7-
[![R-CMD-check](https://github.com/ropensci/plotly/workflows/R-CMD-check/badge.svg)](https://github.com/plotly/plotly.R/actions)
8-
[![CRAN
9-
Status](https://www.r-pkg.org/badges/version/plotly)](https://cran.r-project.org/package=plotly)
10-
[![CRAN
11-
Downloads](https://cranlogs.r-pkg.org/badges/grand-total/plotly)](https://www.rpackages.io/package/plotly)
12-
[![monthly](https://cranlogs.r-pkg.org/badges/plotly)](https://www.rpackages.io/package/plotly)
13-
<!-- badges: end -->
14-
15-
An R package for creating interactive web graphics via the open source
16-
JavaScript graphing library
17-
[plotly.js](https://github.com/plotly/plotly.js).
18-
19-
## Installation
20-
21-
Install from CRAN:
22-
23-
``` r
24-
install.packages("plotly")
25-
```
26-
27-
Or install the latest development version (on GitHub) via `{remotes}`:
28-
29-
``` r
30-
remotes::install_github("plotly/plotly")
31-
```
32-
33-
## Getting started
34-
35-
### Web-based ggplot2 graphics
36-
37-
If you use [ggplot2](https://github.com/tidyverse/ggplot2), `ggplotly()`
38-
converts your static plots to an interactive web-based version\!
39-
40-
``` r
41-
library(plotly)
42-
g <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
43-
stat_density_2d(aes(fill = ..level..), geom = "polygon") +
44-
xlim(1, 6) + ylim(40, 100)
45-
ggplotly(g)
46-
```
47-
48-
![<https://i.imgur.com/G1rSArP.gifv>](https://i.imgur.com/G1rSArP.gif)
49-
50-
By default, `ggplotly()` tries to replicate the static ggplot2 version
51-
exactly (before any interaction occurs), but sometimes you need greater
52-
control over the interactive behavior. The `ggplotly()` function itself
53-
has some convenient “high-level” arguments, such as `dynamicTicks`,
54-
which tells plotly.js to dynamically recompute axes, when appropriate.
55-
The `style()` function also comes in handy for *modifying* the
56-
underlying trace
57-
attributes (e.g. [hoveron](https://plotly.com/r/reference/#scatter-hoveron)) used to generate the plot:
58-
59-
``` r
60-
gg <- ggplotly(g, dynamicTicks = "y")
61-
style(gg, hoveron = "points", hoverinfo = "x+y+text", hoverlabel = list(bgcolor = "white"))
62-
```
63-
64-
![<https://i.imgur.com/qRvLgea.gifv>](https://imgur.com/qRvLgea.gif)
65-
66-
Moreover, since `ggplotly()` returns a plotly object, you can apply
67-
essentially any function from the R package on that object. Some useful
68-
ones include `layout()` (for [customizing the
69-
layout](https://plotly-r.com/improving-ggplotly.html#modifying-layout)),
70-
`add_traces()` (and its higher-level `add_*()` siblings, for example
71-
`add_polygons()`, for [adding new
72-
traces/data](https://plotly-r.com/improving-ggplotly.html#leveraging-statistical-output)),
73-
`subplot()` (for [combining multiple plotly
74-
objects](https://plotly-r.com/arranging-views.html#arranging-plotly-objects)),
75-
and `plotly_json()` (for inspecting the underlying JSON sent to
76-
plotly.js).
77-
78-
The `ggplotly()` function will also respect some “unofficial”
79-
**ggplot2** aesthetics, namely `text` (for [customizing the
80-
tooltip](https://plotly-r.com/controlling-tooltips.html#tooltip-text-ggplotly)),
81-
`frame` (for [creating
82-
animations](https://plotly-r.com/animating-views.html)),
83-
and `ids` (for ensuring sensible smooth transitions).
84-
85-
### Using plotly without ggplot2
86-
87-
The `plot_ly()` function provides a more direct interface to plotly.js
88-
so you can leverage more specialized chart types (e.g., [parallel
89-
coordinates](https://plotly.com/r/parallel-coordinates-plot/) or
90-
[maps](https://plotly.com/r/maps/)) or even some visualization that the
91-
ggplot2 API won’t ever support (e.g., surface,
92-
[mesh](https://plotly.com/r/3d-mesh/),
93-
[trisurf](https://plotly.com/r/trisurf/), etc).
94-
95-
``` r
96-
plot_ly(z = ~volcano, type = "surface")
97-
```
98-
99-
![<https://plot.ly/~brnvg/1134>](https://plot.ly/~brnvg/1134.png)
100-
101-
## Learn more
102-
103-
To learn more about special features that the plotly R package provides (e.g., [client-side linking](https://plotly-r.com/client-side-linking.html), [**shiny** integration](https://plotly-r.com/linking-views-with-shiny.html), [editing and generating static images](https://plotly-r.com/publish.html), [custom events in JavaScript](https://plotly-r.com/javascript.html), and more), see <https://plotly-r.com>. You may already be familiar with existing plotly documentation (e.g., <https://plotly.com/r/>), which is essentially a language-agnostic how-to guide for learning plotly.js, whereas <https://plotly-r.com> is meant to be more wholistic tutorial written by and for the R user. The package itself ships with a number of demos (list them by running `demo(package = "plotly")`) and shiny/rmarkdown examples (list them by running `plotly_example("shiny")` or `plotly_example("rmd")`). [Carson](https://cpsievert.me) also keeps numerous [slide decks](https://talks.cpsievert.me) with useful examples and concepts.
104-
105-
## Contributing
106-
107-
Please read through our [contributing
108-
guidelines](https://github.com/plotly/plotly.R/blob/master/CONTRIBUTING.md).
109-
Included are directions for opening issues, asking questions,
110-
contributing changes to plotly, and our code of
111-
conduct.
112-
113-
-----
114-
115-
![<https://ropensci.org>](https://www.ropensci.org/public_images/github_footer.png)
1+
This is a fork of the ropensci/plotly HtmlWidget for use in Displayr. The repository has been modified to be an node package and use the rhtmlBuildUtils framework. This was done in order to run unit tests on the widget js code, which has been modified by Displayr. The integration with rhtmlBuildUtils is incomplete, and only the building and unit testing of widget js code is supported. Features from rhtmlBuildUtils such as visual regression testing, linting and the internal web server are absent.

build/config/widget.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const cliArgs = require('yargs').argv
2+
const _ = require('lodash')
3+
4+
const config = {
5+
widgetEntryPoint: 'theSrc/scripts/plotly.js',
6+
widgetFactory: 'theSrc/scripts/plotly.factory.js',
7+
widgetName: 'plotly',
8+
}
9+
10+
const commandLineOverides = _.omit(cliArgs, ['_', '$0'])
11+
module.exports = _.merge(config, commandLineOverides)

gulpfile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const gulp = require('gulp')
2+
const rhtmlBuildUtils = require('rhtmlBuildUtils')
3+
4+
const dontRegisterTheseTasks = []
5+
rhtmlBuildUtils.registerGulpTasks({
6+
gulp: gulp,
7+
exclusions: dontRegisterTheseTasks
8+
})

inst/htmlwidgets/plotly.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dependencies:
2+
3+
- name: Maps
4+
version: 1.0.0
5+
src: htmlwidgets
6+
script:
7+
- plotly.js.map

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "plotly",
3+
"private": true,
4+
"engines": {
5+
"node": ">=12.9.0"
6+
},
7+
"scripts": {
8+
"preinstall": "([ \"$CI\" != true ] && npx npm-force-resolutions) || true"
9+
},
10+
"devDependencies": {
11+
"gulp": "^4.0.2",
12+
"husky": "^0.13.3",
13+
"npm-force-resolutions": "0.0.10",
14+
"rhtmlBuildUtils": "github:Displayr/rhtmlBuildUtils#7.2.3"
15+
},
16+
"resolutions": {
17+
"glob-parent": "5.1.2",
18+
"ini": "1.3.8",
19+
"minimist": "1.2.5",
20+
"y18n": "4.0.3",
21+
"yargs-parser": "18.1.3"
22+
}
23+
}

theSrc/scripts/plotly.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import widgetDefinition from './widgetdefinition'
2+
3+
HTMLWidgets.widget(widgetDefinition)

theSrc/scripts/vis-992.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Unit test for VIS-992
2+
// Check that resize() works even when element is not attached to the DOM.
3+
// We create a mock of the Plotly object that has the relayout function that throws an
4+
// error if an ID is supplied for the element and it is not attached to the DOM.
5+
6+
const widgetDefinition = require('./widgetdefinition.js')
7+
8+
global.Plotly = {
9+
relayout: el => {
10+
if (typeof el === 'string') {
11+
elementInDocument = document.getElementById(el);
12+
if (elementInDocument === null) {
13+
throw new Error('No DOM element with id \'' + el + '\' exists on the page.');
14+
}
15+
}
16+
}
17+
}
18+
19+
test('test that', () => {
20+
const div = document.createElement('div')
21+
div.id = 'ID'
22+
expect(document.getElementById(div.id)).toBe(null)
23+
24+
const instance = {autosize: true, width: 0, height: 0}
25+
widgetDefinition.resize(div, 0, 0, instance) // this used to error before VIS-992
26+
})

0 commit comments

Comments
 (0)