Closed ghost closed 4 years ago
If I move the grid::convertUnit calls into the lp.plot() reactive, where the plot_grid function is called, those calls are safe. Perhaps the problem is invoking grid::convertUnit on a grob that may have been created in a different viewport? I think I can achieve my desired functionality by restricting all my calls to grid::convertUnit within the same context in which the plotting functions are called (i.e., lp.plot()), but I'm still surprised that invoking the function in the wrong place induces this sporadic and persistent rendering bug. The fact that it requires an OS restart makes me think some internal state of the graphics engine is being corrupted, which seems like a bug, particularly in a base R package.
I've now tested and validated a solution that circumvents this problem. The solution is actually better designed code, rather than a hack/workaround, so I'm OK if this issue is closed. In general, I admit that one should avoid calling conversion routines on grobs in a context where the viewport may have changed since plot creation. But there could be use cases where this is desired, for instance when the change in viewport is properly taken into account. And the strange persistence of the bug may bewilder unsuspecting application developers. I'm also troubled by the possibility that simply calling the conversion utilities in the wrong place is corrupting some internal data in the graphics engine.
Also, I'm not sure whether the problem is: 1.) calling the conversion utility on a grob after a change in viewport; or 2.) calling the conversion utility in a mouseover event callback
It sounds like this is a bug in R itself, or possibly in the grid package. I suggest trying to create a minimal reproducible example (without shiny) and posting it to the R-devel mailing list, or filing in the R bugzilla tracker.
Closing since it appears you've found a workaround
I've now tested and validated a solution that circumvents this problem. The solution is actually better designed code, rather than a hack/workaround, so I'm OK if this issue is closed.
System details
Browser Version: NA
Output of
sessionInfo()
:Example Application
Steps to Reproduce
To reproduce the problem, please follow these steps: 1.) In the function get_x_y_values, please uncomment the line "#from_top <- sum(to_mm(gt$heights[seq(panel_pos$t - 1)]))" But note that the bug caused by this line may require you to restart your computer to fix it (i.e., comment out the line, restart the OS, then re-run the app)
2.) Run the shiny app. (I do this in RStudio.)
3.) Choose "b1" (or "b2", it doesn't matter) from the dropdown menu.
4.) Click the Plot button. The plot will be empty. This is the first sign of trouble. Maximize the window and the plot will become visible.
5.) Remove the "b1" selection and click the Plot button again. This will clear the plot correctly.
6.) Select "b1" and plot again. The plot will be empty.
If you follow these steps with the troublesome line commented out--assuming the bad code has not run without restarting the OS--this is the expected behavior: 4.) Click the Plot button. The plot will render regardless of the initial window size. Maximize/minimize the window; the plot remains visible and is correctly resized.
6.) Select "b1" and plot again. The plot should appear just as it did the first time.
App and Bug Description
The Plot button, when clicked, generates a ggplot and a Gviz plot, tied together by cowplot::plot_grid. The tricky part is that I've implemented functionality to report information about the points in the ggplot whenever the user hovers the mouse over the plot. Since the plot is actually a composite of the ggplot and another object from the Gviz package, this functionality requires some transformations from the coordinates reported by the hover info to the coordinates of the points in the underlying plot object. To compute that transformation, I make calls to grid::convertUnit in the callback function that gets invoked on mouseover events. Even a single call to grid::convertUnit in this context causes the sporadic rendering described above. Interestingly, the problem persists after I comment out the offending line of code. It persists even when I close the RStudio session and restart!! I have to actually restart my OS (I'm running Ubuntu 16.04) to get back to the original behavior.