7

I always start my scripts with:

rm(list=ls())

to clear my workspace to avoid conflicts between different scripts. However, I'm looking for a way to also set all changed options to their default state. For instance, in some scripts I need to change the SS type by setting:

options(contrasts=c(unordered="contr.sum", ordered="contr.poly"))

In other scripts I need to use the default option (and since it is default I do not specify it directly) that is:

options(contrasts=c(unordered="contr.treatment", ordered="contr.poly"))

but if a script with the changed options was just used before, the option is obviously changed without me noticing it.

Is there a command that I could put on top of my scripts to reset R to default options?

1
  • Yes, something like, op <- options(...); your stuff here; options(op).
    – chl
    Apr 11, 2013 at 10:56

4 Answers 4

11

Like @chl said, you can save your default options somewhere, e.g. in an Rdata file using save:

default_options = options()
save(default_options, file = "default_options.rda")

Now you can load those defaults from the file and apply them:

load("default_options.rda")
options(default_options)

However, I would advice against running R scripts serially. In stead, if you have shared functionality between scripts, simply create functions that capture that functionality. The script you work in is then always self-contained, no influence from other, previously run scripts. Any options you set are then local to that script, and there is no need to empty your workspace or set your options to default.

Note that I also never save my working environment, I always reconstruct my environment using the raw data and scripts that transform that raw data. If there are parts that take a lot of time, I put them in a separate script which at the end save an Rdata file using save. I then load them in the main script using load.

3

Here is an easy and elegant one-liner that doesn't require any manual retrieval of defaults:

default_opts <- callr::r(function(){options()}); options(default_opts)

How this works:

callr is a package that comes with devtools (so if you've got devtools installed, you'll already have callr)

callr::r simply starts a background processes, so from that, we can retrieve the default options with options() and return them to the current R session

Example

# Default option
options("scipen")
# $scipen
# [1] 0

# Set to something else
options(scipen = 999)
# $scipen
# [1] 999

# Reset to defaults:
default_opts <- callr::r(function(){options()}); options(default_opts)


# Did it work? yes, the option is back to its default value
options("scipen")
# $scipen
# [1] 0
1
1

If you use Rstudio project functionality, you will get rid of all your problems at once. see https://www.tidyverse.org/articles/2017/12/workflow-vs-script/ for more details about why you should avoid rm(list=ls())

0

It is widely believed that the following will avoid conflicts between scripts.

rm(list=ls())

It will not. If you do not believe this, try the following

library(dplyr)
library(raster)

rm(list = ls())

library(dplyr)

tibble::tibble(x = 1:10, y = 1:10) %>% 
  select(x)

select should be from dplyr. But it is from raster. The reason? rm(list = ls()) does not remove loaded libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.