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)
attach(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.
Many thanks for sharing so many good ideas and advice!
ReplyDeleteHolger
I think I get more useful R tips from reading people's .Rprofiles's than any where else!
ReplyDeleteTHANKS!
Nice idea! After looking at yours I just made "d" an alias for "data.frame" with:
ReplyDeleted <- 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.
this is awesome! thanks
ReplyDeleteSamantha
Good idea a Tom. I would love to see a Rprofile sharing group on Github something.
ReplyDeleteThanks 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.
ReplyDeleteA group on Github would be excellent. I haven't used it, but maybe I'll have to savvy myself and create the group.
ReplyDeleteTom - Great - if you figure something out let me know and I'll post about it here.
ReplyDeleteCould you please explain the reason for using special characters in your "%nin%" function?
ReplyDeleteRegards,
Ram
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.
ReplyDelete