Ryan Peek on Customizing Your R Setup

[This article was first published on Noam Ross - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

To leave a comment for the author, please follow the link and comment on their blog: Noam Ross - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)