R with RStudio and Sublime Text on Mac OS X21 Jan 2012
R is free and open-source software for statistical computing and graphics. Learning how to use R requires effort, but pays accordingly. This quick guide to R on Mac OS X collects some essential information on how to get started and build your first R extension.
1. How to install and use R
1.1 Install R
Download and install the latest version of R.
After opening R you may run into this error message. As a remedy go to your Mac’s Terminal and enter:
defaults write org.R-project.R force.LANG en_US.UTF-8
1.2 Use an editor
You can use R’s own console, but you won’t regret something more fancy.
1.2.2 Sublime Text
Open Sublime and hit CMD-Shift-P, type install, and choose ‘Package Control: Install Package’. Search for ‘R-Box’ and install it. Repeat this step for ‘R-snippets’ and ‘SublimeREPL’. You can now start an R session in Sublime by going to Tools > SublimeREPL > R.
Finally you need to make sure that any R syntax you run from within Sublime is sent to the R session in Sublime: hit CMD-Shift-P again, type rbox, and choose ‘R-Box: Choose Program’. Select ‘SublimeREPL’. Now simply hit CMD-Enter on a line of R code to send it to the Sublime R session.
1.3 Learn to use
An Introduction to R – The official introductory manual and a great reference.
R for Beginners – A manual for beginners of R.
R for Absolute Beginners – A manual for absolute beginners of R.
Computational Statistics – Excellent lecture notes from a course on R.
Introductory Statistics with R – A free online book by Peter Dalgaard.
1.4 Get help
R manuals – All official manuals for R.
RSeek – A search engine for R.
Cheat sheet – Two reference cards for R.
Quick-R – Great online reference for R.
StackOverflow – Excellent forum for posting questions about R.
2. How to build an R package
2.1 Set up
Source the functions that should be included in your package. It is good practice to write each function in a separate file and give the file and function the same appropriate name. To source the functions, either open the files in RStudio and source them using the source-button, or use the
Now build the skeleton of your package:
package.skeleton(list = c("functionNameA", "functionNameB", "functionNameC"), name = "packageName")
Find your package skeleton in a directory named after your package in your current working directory. Use the
getwd() function if you don not remember what your current working directory is.
2.2 Write supporting files
The skeleton includes a DESCRIPTION and NAMESPACE file, and a man (hosting the manuals) and R (hosting the functions) directory. Optionally, you may add a data directory, which you may use to include an .Rdata-file for instance.
First open and update the DESCRIPTION file. This file should be self-explanatory, but instructions and an example can be found here.
Finally make sure you write the manual files. Consider using any of these keywords in your manual.
As these steps might not be trivial the first time, consider having a look at someone else’s package. Find a package, download and open the packageName_versionNumber.tar.gz file, and open the package files to see how it should be done.
2.3 Build package
You may now build the package. Open the Terminal (Applications > Utilities > Terminal) and run (fill in the right names and omit the stars):
cd /Users/*...*/*parent_directory_of_package_directory*/ R CMD build *packageName* R CMD check *packageName*
Be sure to use the correct directory and name of your package. If necessary solve the encountered problems, which are described in the 00install.out file in the packageName.Rcheck directory. Build and check your package until it is ready for use. Your package is called packageName_versionNumber.tar.gz.
If you wish to submit your package to CRAN, please refer to the manual.