nx10 / httpgd

Asynchronous http server graphics device for R.
https://nx10.github.io/httpgd
GNU General Public License v2.0
379 stars 19 forks source link

Reset par on clear #15

Closed renkun-ken closed 2 years ago

renkun-ken commented 4 years ago

Does it make sense to reset par on clear (RStudio behavior)?

For, example, the following code uses par() before plot.

httpgd::httpgd()
utils::browseURL(httpgd::httpgdURL())
par(mfrow = c(2, 2))
m <- lm(mpg ~ cyl, data = mtcars)
plot(m)

Then if users clicks Clear button, then the plot is gone.

The difference appears when user then creates a new simple plot:

plot(rnorm(100))

In RStudio, when the plot is all cleared, the par is reset too so that a new plot will revert to par(mfrow = c(1, 1)). In httpgd at the moment, it is not reverted to default par.

I'm wondering which makes more sense?

nx10 commented 4 years ago

The "clean" button in RStudio kills the graphics device which results in par being reset. This seems to be the recommended way of resetting par (https://stackoverflow.com/questions/5789982/reset-par-to-the-default-values-at-startup). In httpgd this would mean that the webserver had to be shut down and restarted, which I think we should avoid. Do you shink we should make a copy of the par arguments as suggested by the second answer of the stackoverflow link:

opar <- par() # on startup
# ...
par(opar) # on "clean"

Or is resetting the mfrow argument after clean sufficient?

par(mfrow = c(1, 1))

I am not exactly sure what other settings of par are commonly used.

MrJGao commented 2 years ago

This is an old session, but I do think it's more convenient if there's a button or command to reset the par. Is it possible to save a default par object when startup and add a button on the toolbar of the plot window to reset par? if that's easier than killing the server and restarting it. Thanks.

nx10 commented 2 years ago

Thank you for your request, I agree and have added it to the 1.3 tracking issue.

nx10 commented 2 years ago

Thank you for waiting patiently. I just added a parameter

hgd(
  ... ,
  reset_par = getOption("httpgd.reset_par", FALSE)
)

that toggles this functionality.