Make Option "vanilla" Default for All R Sessions

Motivation

It is usually recommended not to save the workspace every time when we quit an R session so that no workspace will be automatically loaded next time. It prevents potential problem especially when someone is unaware of that the previous workspace is loaded automatically in the current R session. It also speeds up the initialization of R without loading possibly large .RData file. Without any customization, R will ask whether we would like to save the workspace whenever we try to close it, which can be really annoying sometimes.

In addition, we may have some customization in the site-wide Rprofile.site or user environment file .Rprofile, which helps us set up an R session catering to our own flavors or convenience. For example, the options("repos") can be set in .Rprofile so that we do not need specify one of the CRAN mirrors whenever installing or updating packages. Certain package, such as colorout for colorful R output in terminal or data.table for efficient data processing can be automatically attached to the global environment as the startup of R session. However, it is possible that we get so used to these sweet tweaks, take them for granted, and forget their existence after a certain period of time. Therefore, we need be careful when checking the reproducibility of the R scripts we wrote.

All the trouble can be avoided by turning on the option --vanilla (or --no-save --no-restore if we want to keep some trivial but sweet customization).

Some Relevant Options of R Command

All the options of R command and their explanation can be found by man R. Some relevant options are given for a quick reference as follows:

--no-save
    Don't save it

--no-environ
    Don't read the site and user environment files

--no-site-file
    Don't read the site-wide Rprofile

--no-init-file
    Don't read the user R profile

--no-restore
    Don't restore anything

--vanilla
    Combine --no-save, --no-restore, --no-site-file, --no-init-file and --no-environ

From man R, the option --vanilla is a combined option of --no-save, --no-restore, --no-site-file, --no-init-file, and --no-environ for a vanilla environment.

Make option --vanilla default

  • We may add an alias of R in .bashrc so that the R session called in terminal or Rstudio will actually have --vanilla turned on by default.
alias R="$(/usr/bin/which R) --vanilla"

It is easy to verify whether the alias does the trick (after source ~/.bashrc). Just start a new R session in the terminal by entering R and type q() to quit. If R does not ask whether you would like to save the workspace, you were actually using R --vanilla.

  • For using ESS, we may customize the R argument by M-x customize-variable, inferior-R-args, and setting it to be --vanilla. Or put the following sample configure into .custom.el directly inside the custom-set-variables as follows:
(custom-set-variables
;; other customization
'(inferior-R-args "--vanilla")
)

To keep some trivial but really sweet customization in .Rprofile, we may consider replacing the option --vanilla with --no-save --no-restore in the steps given above.

comments powered by Disqus