Ryan Peek on Customizing Your R Setup

by Noam Ross, 02 November 2012


Ryan Peek showed us how to use an .Rprofile file to customize your R setup. Here are his instructions and script:

For Windows

  • To change profile for R, go here:

    C:\Program Files\R\R-2.15.1\etc (or whatever version you are using)

  • Edit the “Rprofile.site” file
  • Restart R

For Macs

  • Create your Rprofile file. -use TextEdit or another editor to create a file called Rprofile.txt

  • In a terminal window, type:

    >> cp Rprofile.txt .Rprofile

    (which copies the visible Rprofile.txt to the invisible .Rprofile)

    • You can check this by doing >ls -la again to see it in the directory listing.
  • Restart R

Note that you can save .Rprofile in your home directory, or, if you are using R Studio, in a project directory. In the latter case, the profile will only be loaded when you load that project.

Things to put in the .Rprofile file:

Set a local CRAN Mirror:

local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.cnr.berkeley.edu/"
       options(repos=r)})

Set machine-specific options: If you use Dropbox or something similar to sync R files across computers, you can use Sys.info() to set options specifically for your machine. This code sets the root path differently for different machines:

if(Sys.info()[4]=="Work-PC") {
  root<-"C://Users//Ryan//Desktop//Dropbox//R//"
} else if(Sys.info()[4]=="Mac.local") {
  root<-"/Users/Ryan/Dropbox/R/"
} else {
  root<-"C://Users//rapeek//Dropbox//R//"
}

Set a welcome message and load a file of other useful functions: Anything in the .First function is run on startup

.First <- function(){
    cat("\nWelcome to R!\n",sep="")
    cat("---------------\n\n",sep="")
    
    if(file.exists("~/RbasicFunctions_example.r")){
        source("~/RbasicFunctions_example.r")
        cat("RbasicFunctions_example.r was loaded, providing the following functions:\n\n",sep="")
        cat("use  'print.functions()'  to view\n",sep="")
    }
}

Helper functions

Here’s Ryan’s file, called above as RbasicFunctions_example.r, which has his helper functions. Running print.functions() will display a list of these with short explanations:

A lot of other useful options for R profile files are found in this Stack Overflow discussion