To install R on OSX, first install Homebrew, and then run

brew install r

Update: It is also recommended to install the pkg-config and cairo libraries:

brew install cairo
brew install pkg-config

If these libraries are not installed, then the ggplot2 package may not install properly with all its dependencies.

Creating a ~/.Rprofile file

It would also help to create a file ~/.Rprofile (on OSX) containing the line:

.libPaths("~/build/rpackages")

where the path specified is the location to which you install additional packages, such as ggplot2.

And remember to create the folder:

mkdir -p ~/build/rpackages

If you want certain packages to be automatically loaded, then your ~/.Rprofile could look something like:

.libPaths("~/path/to/Rpackages")

#to avoid pop-up for mirror when installing packages
options(menu.graphics=FALSE)

Packages to install

  • xtable: install this first
  • devtools: important
  • roxygen2: important
  • testthat: important
  • Rcpp: important
  • ggplot2: make sure pkg-config and cairo are installed
install.packages("xtable", lib="~/build/rpackages/")

then something like:

install.packages("ggplot2", lib="~/build/rpackages/", dependencies=T)
Now you can modify your ~/.Rprofile file to look something like:
.libPaths("~/path/to/Rpackages")
library(devtools)
library(ggplot2)

#to avoid pop-up for mirror when installing packages
options(menu.graphics=FALSE)

Fixing the LC_* errors

However, when you start up R using

R

you will probably see see following at the end of the R startup messages:

During startup - Warning messages:
1: Setting LC_COLLATE failed, using "C"
2: Setting LC_TIME failed, using "C"
3: Setting LC_MESSAGES failed, using "C"
4: Setting LC_MONETARY failed, using "C"

To stop these error messages appearing, include the following line in your ~/.bash_profile file (on OSX), or create the file if it does not exist to contain the following:

# export LANG=en_US.UTF-8
# export LC_ALL=en_US.UTF-8

I got this solution from this post. Although I think only the first line (export LANG...) is needed, it does not hurt to include both.

As usual, you will probably need to close/open a new terminal in order for this change to take effect.