diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js
index 7deca6f8d1..e74f32b405 100644
--- a/inst/htmlwidgets/plotly.js
+++ b/inst/htmlwidgets/plotly.js
@@ -17,6 +17,14 @@ HTMLWidgets.widget({
renderValue: function(el, x, instance) {
+ // Plotly.relayout() mutates the plot input object, so make sure to
+ // keep a reference to the user-supplied width/height *before*
+ // we call Plotly.plot();
+ var lay = x.layout || {};
+ instance.width = lay.width;
+ instance.height = lay.height;
+ instance.autosize = lay.autosize || true;
+
/*
/ 'inform the world' about highlighting options this is so other
/ crosstalk libraries have a chance to respond to special settings
@@ -153,17 +161,9 @@ HTMLWidgets.widget({
var plot = Plotly.plot(graphDiv, x);
instance.plotly = true;
- instance.autosize = x.layout.autosize || true;
- instance.width = x.layout.width;
- instance.height = x.layout.height;
} else {
- // new x data could contain a new height/width...
- // attach to instance so that resize logic knows about the new size
- instance.width = x.layout.width || instance.width;
- instance.height = x.layout.height || instance.height;
-
// this is essentially equivalent to Plotly.newPlot(), but avoids creating
// a new webgl context
// https://github.com/plotly/plotly.js/blob/2b24f9def901831e61282076cf3f835598d56f0e/src/plot_api/plot_api.js#L531-L532