Reset par to the default values at startup
Asked Answered
E

6

90

Normally when I make my own plot functions, I make a construct :

op <- par("mypar"=myvalue)
on.exit(par(op))

which is the standard way of reverting the par to the previous values. Imagine you've been running some functions that did change some of the pars, and you need to reset to the default values at startup in R. What is the convenient way of doing so?

Or in other words : how does one reaches the default values for par()?

Enjoyment answered 26/4, 2011 at 12:1 Comment(4)
I was under the impression that each time a new graphics device gets started it is given default values anyway.Cogent
@Cogent They do, but I think the thrust of @Joris Q is, is there a way to reset the pars for the current device if one hasn't saved the defaults. In other words, how do we determine the default pars for a device, not the current pars.Kurt
@Gavin Simpson Ah ok, but dev.new() and dev.off() could be used thenCogent
@Cogent Indeed; I mentioned this to @Joris in the R tag chat room, but we want a less hacky solution :-)Kurt
D
121

Every time a new device is opened par() will reset, so another option is simply do dev.off() and continue.

Dodd answered 9/8, 2015 at 21:28 Comment(2)
Amongst all the provided replies, this is the only one that actually answers the question asked.Gem
@Dodd wouldn't dev.off() be a bad option if we were to have multiple plots in a pdf ? because this would close the pdf device and so won't be able to have multiple plots in a single pdfAlice
K
54

This is hacky, but:

resetPar <- function() {
    dev.new()
    op <- par(no.readonly = TRUE)
    dev.off()
    op
}

works after a fashion, but it does flash a new device on screen temporarily...

E.g.:

> par(mfrow = c(2,2)) ## some random par change
> par("mfrow")
[1] 2 2
> par(resetPar())     ## reset the pars to defaults
> par("mfrow")        ## back to default
[1] 1 1
Kurt answered 26/4, 2011 at 12:39 Comment(6)
It might be safer to avoid attempting to reset items that would have been changed by resizing such as mai, mar, pin, plt and pty (as is cautioned in the par help page.)Toreador
A similar idea was discussed on R-help: tolstoy.newcastle.edu.au/R/e2/help/07/09/26665.htmlRhodos
Didn't find anything else, so that'll have to do it.Enjoyment
@Thermaesthesia In what sense doesn't this work with RStudio? Is it something to do with their device and the code above starting a new one?Kurt
I get the following error message 'Warning message: In (function () : Only one RStudio graphics device is permitted'Thermaesthesia
@Thermaesthesia Tough then; there can only be one such device and as R doesn't store the defaults there is now way, esily, to grab them. Ask the RStudio people about this. Alternatively, don't change par() without storing the defaults. Or arrange for the defaults to be grabbed at start-up through your .Rprofile.Kurt
V
24

From Quick-R

par()              # view current settings
opar <- par()      # make a copy of current settings
par(col.lab="red") # red x and y labels 
hist(mtcars$mpg)   # create a plot with these new settings 
par(opar)          # restore original settings
V2 answered 31/5, 2013 at 14:41 Comment(2)
I know that one, but it doesn't restore the default values if you changed them before. par() shows the current settings, not the default settings.Enjoyment
This solution may not be working in this case but let's leave it here as an approach that does not work.V2
M
4

An alternative solution for preventing functions to change the user par. You can set the default parameters early on the function, so that the graphical parameters and layout will not be changed during the function execution. See ?on.exit for further details.

on.exit(layout(1))
opar<-par(no.readonly=TRUE)
on.exit(par(opar),add=TRUE,after=FALSE)
Monkeypot answered 14/10, 2019 at 0:40 Comment(0)
A
2

dev.off() is the best function, but it clears also all plots. If you want to keep plots in your window, at the beginning save default par settings:

def.par = par()

Then when you use your par functions you still have a backup of default par settings. Later on, after generating plots, finish with:

par(def.par) #go back to default par settings

With this, you keep generated plots and reset par settings.

Anthelmintic answered 23/4, 2020 at 15:48 Comment(1)
Be aware that some of the graphical parameters are read-only and thus cannot be reset. Thus, your initial blanket call to par is best done as par(no.readonly = TRUE).Pinpoint
E
1

Use below script to get back to normal 1 plot:

par(mfrow = c(1,1))
Ean answered 8/8, 2018 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.