The Rfuncs Project
Installing Rfuncs
Rfuncs can be stored on your hard disk in a format that lets them be easily viewed and used anytime—much like functions distributed as an R package, in some ways, even better. The two basic set-ups are:
- "User-loadable" Rfuncs. The Rfunc can be loaded quickly when needed. This is analogous to using R's library() or require() command to load and attach an R package already downloaded and installed from CRAN. (Why Is the Rfunc Project Not a CRAN Package?)
- "Auto-loading" Rfuncs. The Rfunc is loaded automatically when RStudio is restarted. In essence, the Rfunc becomes part of the user's base R system, ready to be viewed and used at any moment. For example, FitterJitter() may fit this category.
"User-loadable" Rfuncs
Saving the Rfunc
I recommend storing user-loadable Rfuncs in a common directory. I call mine RfuncsSaved, which is a sub-directory of my RfuncsProject directory, as discussed in Using Rfuncs. You may prefer another structure.
Let us pretend you are saving the Rfunc called AgeDays (the focus in Using Rfuncs), and AgeDaysRfuncYYMMDD.R has already been sourced. Thus, AgeDays is already shown in the Environment pane.
Saving the Rfunc
I recommend storing user-loadable Rfuncs in a common directory. I call mine RfuncsSaved, which is a sub-directory of my RfuncsProject directory, as discussed in Using Rfuncs. You may prefer another structure.
Let us pretend you are saving the Rfunc called AgeDays (the focus in Using Rfuncs), and AgeDaysRfuncYYMMDD.R has already been sourced. Thus, AgeDays is already shown in the Environment pane.
When R processes a save() command, it builds a file in the current Working Directory (a formal term in R). This needs to be your RfuncsSaved directory (or whatever you named it). In RStudio, click Session --> Set Working Directory --> Choose Directory and locate that directory on your computer.
In RStudio's Console pane, you should see something like:
setwd("~/RfuncsProject/RfuncsSaved")
In RStudio's Console pane, issue this command:
> save(AgeDays, file="AgeDays.RfuncYYMMDD")
where YYMMDD is the date stamp for the version.
Now you will see AgeDays.RfuncYYMMDD in your RfuncsSaved directory:
> save(AgeDays, file="AgeDays.RfuncYYMMDD")
where YYMMDD is the date stamp for the version.
Now you will see AgeDays.RfuncYYMMDD in your RfuncsSaved directory:
Loading and using a saved Rfunc
Now let's pretend you need to use AgeDays for another analysis. (Reading these steps might take more time than doing them!)
- To start anew, exit and restart RStudio.
- AgeDays should not be in the Environment pane.
- Click: Session --> Load Workspace
- Click to your RfuncsSaved directory and click on AgeDays, as per previous screenshot.
- AgeDays will now appear in the Environment pane and thus is ready to use just like any base R function.
- At the Console pane's command line, use AgeDays to compute how many days old you are today. For Barrack Obama (as of 24 February 2016):
> AgeDays("1961-08-04")
Age in days: 19927
It's easy, eh?
"Auto-loading" Rfuncs
Saving the Rfunc
The Rfunc will be saved in a file named .RData in your default working directory for R. You may now need to create one. Here, I show it being "mainRworkingdirectory". Clicking Tools --> Global Options --> General will allow you to (1) set the path, and (2) tell R to auto-load the objects in .RData at startups. Also, I recommend (3) clicking "Never", as shown, because (at least for me) automatically saving my entire workspace when exiting RStudio would induce pathological hoarding disorder of R objects. Click "OK" to save these settings.
Saving the Rfunc
The Rfunc will be saved in a file named .RData in your default working directory for R. You may now need to create one. Here, I show it being "mainRworkingdirectory". Clicking Tools --> Global Options --> General will allow you to (1) set the path, and (2) tell R to auto-load the objects in .RData at startups. Also, I recommend (3) clicking "Never", as shown, because (at least for me) automatically saving my entire workspace when exiting RStudio would induce pathological hoarding disorder of R objects. Click "OK" to save these settings.
Quit RStudio and restart it by double clicking on the icon for the Rfunc file you wish to install, in this case, AgeDaysRfuncYYMMDD.R, where YYMMDD is the date-stamp for the version. RStudio will open with this file active.
Click Source.
The Environment panel will now show AgeDays. There may also be other objects, which were auto-loaded from .RData. You can remove any by executing rm(ObjectName) in the Console pane. Tidy up R's current workspace (the objects shown in Environment) so that it has the objects you want auto-loaded when starting RStudio and is not cluttered with anything else.
Execute
save.image()
in the Console pane.
Now every object that is currently in your workspace environment will be auto-loaded the next time you start RStudio.
Using auto-loaded Rfuncs
In essence, this gives the Rfunc the same usability as any base R function, such as mean().
To check things out, exit and restart RStudio. The Rfunc should be listed in the Environment pane. Clicking on it will show exactly what's in the whole file, including the arguments and examples.
Using AgeDays, you can immediately compute how many days old you are today. For Barrack Obama (as of 24 February 2016):
> AgeDays("1961-08-04")
Age in days: 19927
It's easy, eh?
.RData files partially hidden
Regrettably, the .RData file created by save.image() is tricky to track down. In Windows 7, one sees a nameless R icon and "R Workspace" as the filetype. Good enough.
Regrettably, the .RData file created by save.image() is tricky to track down. In Windows 7, one sees a nameless R icon and "R Workspace" as the filetype. Good enough.
Using a Mac, .RData is invisible, unless you use Unix (in the Terminal application) to drill down to the default working directory and then enter "ls -a .RData", something very few Mac users will ever want or need to do.