Monday, August 15, 2011

Sync Your Rprofile Across Multiple R Installations

Your Rprofile is a script that R executes every time you launch an R session. You can use it to automatically load packages, set your working directory, set options, define useful functions, and set up database connections, and run any other code you want every time you start R.

If you're using R in Linux, it's a hidden file in your home directory called ~/.Rprofile, and if you're on Windows, it's usually in the program files directory: C:\Program Files\R\R-2.12.2\library\base\R\Rprofile. I sync my Rprofile across several machines and operating systems by creating a separate script called called syncprofile.R and storing this in my Dropbox. Then, on each machine, I edit the real Rprofile to source the syncprofile.R script that resides in my Dropbox.

One of the disadvantages of doing this, however, is that all the functions you define and variables you create are sourced into the global environment (.GlobalEnv). This can clutter your workspace, and if you want to start clean using rm(list=ls(all=TRUE)), you'll have to re-source your syncprofile.R script every time.

It's easy to get around this problem. Rather than simply appending source(/path/to/dropbox/syncprofile.R) to the end of your actual Rprofile, first create a new environment, source that script into that new environment, and attach that new environment. So you'll add this to the end of your real Rprofile on each machine/installation:

my.env <- new.env()
sys.source("C:/Users/st/Dropbox/R/Rprofile.r", my.env)

All the functions and variables you've defined are now available but they no longer clutter up the global environment.

If you have code that you only want to run on specific machines, you can still put that into each installation's Rprofile rather than the syncprofile.R script that you sync using Dropbox. Here's what my syncprofile.R script looks like - feel free to take whatever looks useful to you.


  1. Many thanks for sharing so many good ideas and advice!

  2. I think I get more useful R tips from reading people's .Rprofiles's than any where else!


  3. Nice idea! After looking at yours I just made "d" an alias for "data.frame" with:

    d <- base::data.frame

    It always bugged me that data.frame is absurdly long compared to c. I just hadn't bothered to make a function that simply renames it.

  4. this is awesome! thanks

  5. Good idea a Tom. I would love to see a Rprofile sharing group on Github something.

  6. Thanks for the environment trick. I always wondered about that and whether the only way to declutter my environment is to create my own package which would be included in the search path. This achieves the same much more painlessly.

  7. A group on Github would be excellent. I haven't used it, but maybe I'll have to savvy myself and create the group.

  8. Tom - Great - if you figure something out let me know and I'll post about it here.

  9. Could you please explain the reason for using special characters in your "%nin%" function?


  10. The ""s are so you can use special characters, like % in the function name. The %nin% function is supposed to mimic the %in% function, more like an operator than a function.


Note: Only a member of this blog may post a comment.

Creative Commons License
Getting Genetics Done by Stephen Turner is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.