Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set R to default options?

Tags:

r

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?

like image 741
VLC Avatar asked Apr 11 '13 10:04

VLC


3 Answers

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.

like image 150
Paul Hiemstra Avatar answered Oct 21 '22 01:10

Paul Hiemstra


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
like image 3
stevec Avatar answered Oct 20 '22 23:10

stevec


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())

like image 1
Julien Colomb Avatar answered Oct 21 '22 00:10

Julien Colomb