Skip to content

R experience

Teemu Säilynoja edited this page Feb 24, 2023 · 1 revision

A collection of hacks to make your R workflow a bit smoother and cooler looking.


.Rprofile

What?

Upon startup, in the absence of any command line flags, R automatically runs some scripts:

  1. Site-wide R startup configuration file/script if it exists.
  2. Next, it will check for .Rprofile file in the current working directory
  3. If step 2 was not run, it will check your home directory for the .Rprofile

Why?

Useful to set up some user preferences. And scripts to run when starting up R.

Example

Here is my (Teemu's) personal .Rprofile, living in my home directory:

# By default use 4 cores.
options(Ncpus = 4L,
        mc.cores = 4L,
        future.plan = "multisession")

# Run these only in interactive sessions (terminal editor etc.)
# i.e. these aren't run when runing .R scripts.
if (interactive()) {
  # Nice colouring of outputs in terminal. Quite customisable.
  # install with devtools::install_github("jalvesaq/colorout")
  require("colorout", quietly = T)

  # By default DON'T save the .Rdata
  q <- function (save="no", ...) {
    quit(save=save, ...)
  }
  # Also, exit should exit.
  exit <- q

  # Enable autocompletion for package names in library() and require() calls.
  utils::rc.settings(ipck = TRUE)

  # By default, I don't need to see all of the posterior samples.
  options(max.print = 100)
  
  # More visible than "+ ", when I forget to close a bracket.
  options(prompt = "> ",
          continue = "... ")
}