Similar to installr, updateR is the package to help updating R on Mac OS. The R code you will need is these 5 lines: (credit goes to jroberayalas for the code) install.packages('devtools') #. Mar 12, 2019 The R Core Team announced yesterday the release of R 3.5.3, and updated binaries for Windows and Linux are now available (with Mac sure to follow soon). This update fixes three minor bugs (to the functions writeLines, setClassUnion, and stopifnot), but you might want to upgrade just to avoid the 'package built under R 3.5.4' warnings you might get for new CRAN packages in the future.
Editor’s Note: This is an updated post that covers how to install themacOS toolchain for versions of R starting at 3.6.z.
For R versions between R:
- 3.0.0 - 3.3.3, please see R Compiler Tools for Rcpp on OS X before R 3.4.0.
- 3.4.0 - 3.4.4, please see R Compiler Tools for Rcpp on OS X before R 3.5.0.
- 3.5.0 - 3.5.3, please see R Compiler Tools for Rcpp on OS X before R 3.6.0.
The objective behind this post is to provide users with information on how to setupthe macOS toolchain for compiling used in the 3.6.* series of R.The post is structured primarily for macOS Catalina (10.15.2) users.
If you previously used either the
clang4
, clang6
,or the macos-rtools
installer, please consider deleting the oldcomponents that were installed.Note: Special care should be taken when removing files with the
rm
command.Make sure the commands and file paths are copied in verbatim to a text editorand, then, run in Terminal.To do so, open the
Terminal
from /Applications/Utilities/
and type:In the prior configuration for R 3.4.x, we did a lot of configuration in
~/.R/Makevars
. From R 3.5.x and up, the configuration method changesto modify the PATH
variable using ~/.Renviron
file and adding compilationflags to ~/.R/Makevars
. Therefore, removing the old files installed toobtain a “fresh” slate is useful for working on installing the toolchain forR 3.6.x.Note: If you are upgrading from R 3.0.0 - R 3.3.3 to R 3.6.x, you mayhave the old CRAN compiled
gfortran4.8.2
binary. Please see the instructionsat Removing the old R gfortran binaryfor assistance in removing the binary.One of the primary ways to setup the R toolchain for compiled codeon macOS is to individually installing each element yourself.
There are three components to the R 3.6.x toolchain based onSection: C.3 macOSof R Installation and Administration. These components are:
- Xcode Command Line Tools (“Xcode CLI”)
- clang7
- gfortran6.1
For historical information about the toolchain, please see:R macOS toolchain evolution
Automatic Installation
The instructions in the next section breakdown how to install the differentcomponents for a toolchain on macOS. If you are unfamiliar with Terminal, pleaseconsider using the unofficial macOS Rtools installer package.
Note: This installer package is community maintained and is not affiliatedwith either R Core or CRAN. Hence, this installer package is unofficial.
Manual Install Guide
This guide provides a step-by-step breakdown of the actions required to setupthe toolchain. As a result, this guide will use both installers and script commandsia
Terminal.app
found in /Applications/Utilities/
. Terminal is macOS’sequivalent to Linux’s shell and Window’s command line. From Terminal, we willinstall only theXCode Command Line Tools (“Xcode CLI”).These provide the system headers used to build the official CRAN binary for R.XCode Command Line Tools
- Open the
Terminal
from/Applications/Utilities/
- Type the following into
Terminal
- Note: Anytime the Xcode CLI toolchain updates, you must re-run thiscommand.
- Press “Install”
- Verify installation by typing into terminal:
Note: If you are on macOS Mojave (10.14) or higher, you will need toadd into
~/.R/Makevars
a set of compilation flags.Special thanks to go out to: Kevin Ushey ofRStudio as well asSebastian Weber (
wds15
) andBen Goodrich (@bgoodri
) ofthe Stan project for their work in helping identify anABI break in macOS Catalina (10.15.z) that resulted in needingboth SHLIB_CXXLDFLAGS
and SHLIB_CXX14LDFLAGS
.Installing the clang7
R binary
The
clang7
binary can be obtained at:from the macOS Tools Page
- Download and run the installer.
- Open the
Terminal
from/Applications/Utilities/
- Type:
Install gfortran6.1
binary
The
gfortran6.1
binary can be obtained at:from the macOS Tools Page
Download and run the installer.
Quick check
To verify that everything is working appropriately, let’s do a quick C++ programusing Rcpp and Armadillo.
First, let’s install
Rcpp
and RcppArmadillo
within R.Create a new file, name the follow:
helloworld.cpp
By adding the
.cpp
extension, the file is viewed as being C++ code.Within the file write:
Compile the function using:
where
'path/to/file/'
is the location containing helloworld.cpp
If everything is installed appropriately, then you should see the following in the console:
In addition, you should have a new function within the global environment scopecalled “hello_world”. You can call this function like a normal R function via:
Common Errors
The following are debugged errors that you may run into.
Why can’t R find the clang7
compiler even though I installed it and setup the ~/.Renviron
?
There are three ways to encounter this issue:
First, the issue will appear as a warning when you first installRcppArmadillo from source. e.g.
The configure script will say:
Secondly, if you try to compile a script locally using the pre-builtCRAN binary for RcppArmadillo, then you will run into:
Third way – and the least common – is to have missing build commands. Thiserror is likely due to an improperly specified
PATH
variable.The fixes for each of these problems can be broken down related to:
- Launching R from terminal
- Do you have any custom bindings, e.g.
alias R ='R --flag1 --flag2'
? - Are there any flags with the R call like
--vanilla
or--no-environ
?- For
--vanilla
, please switch to--no-save --no-restore --no-site-file --no-init-file
. - Otherwise, please remove
--no-environ
from the list.
- For
- Do you have any custom bindings, e.g.
- Poorly formatted
~/.Renviron
- Are there multiple
PATH
variables or just one?- If so, delete and/or merge together the multiple copies of
PATH
.
- If so, delete and/or merge together the multiple copies of
- Did you type the path correctly in
~/.Renviron
?- Verify
PATH='/usr/local/clang7/bin:${PATH}'
- Verify
- Are there multiple
- Location of Installation
- Did you install the
clang-7.0.0.pkg
?- Check if a folder is present:
ls /usr/local/clang7
- Check if a folder is present:
- Did you install the