In order to be able to build R packages on Windows, one must install a set of tools that will enable e.g. compilation of C code and LaTeX documents.
For more information see e.g.:
- https://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset
- https://cran.r-project.org/bin/windows/Rtools/
- http://stackoverflow.com/a/33925547/5841613
Obviously, if one is developing R packages, one would want to be able to build them as well. However, there are reasons why users who do not develop R packages might want to be able to build them.
Typically, when installing packages from CRAN (the official R package
repository), the pre-built packages are older than the available source packages
and often lack functionality or improvements that one requires. Source packages
can also be built directly from the development repository (typically GitHub)
using devtools
in case one wants even newer version of the package. Being
able to build packages straight from the development repository means that one
can use packages that are not yet available on CRAN.
R tools is an archive of various command line tools that are native to
Unix/Linux systems, e.g. cat
, diff
and grep
. R tools also
includes gcc, the GNU compiler system.
Download the appropriate version of R tools from the CRAN website:
https://cran.r-project.org/bin/windows/Rtools/
Make note of the path you install to as you will need it later but the default
(c:\Rtools
) should be fine if you have access to it.
MiKTeX must be installed In order to be able to compile LaTeX documents. When building packages, R automatically generates a LaTeX document and compiles it. Even if you do not plan on using LaTeX on your own you must have it installed or else the build process will fail.
Due to access restrictions (not allowed to write to the registry), I was unable to install the default MiKTeX package, even for "this user only". However, the portable version works just fine. Go to the MiKTeX website and download the self extracting archive:
Unzip to a location of your choice but remember to note the path as you will
need it later. My path of choice was c:\MiKTeX 2.9\
.
In order for R to find R tools and MiKTeX, the executables must be in a
directory defined in the Path environment variable. Press the start button and
type environment
. You should find an option named Edit environment
variables for your account. Press that and a window should open.
Under User variables for %username% add a new variable called Path
and, adjusting the paths as needed, set the value to:
C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;C:\MiKTeX 2.9\miktex\bin;C:\Program Files\R\R-3.0.2\bin\x64;
This will be added to your system Path
variable. You may or may not need
to log out and back in for the changes to take effect. You can test whether the
changes have taken effect by opening a terminal (cmd) window and typing:
echo %path%
Once you got the path set correctly, check if R can find R tools by opening R and running:
devtools::find_rtools(debug = TRUE)
Note that you must have the devtools
package installed for this to work.
If you don't, just run install.packages("devtools")
first.
R comes with its own stylesheet for LaTeX documents called Rd.sty. This stylesheet is used for all R documentation. However, MiKTeX will not find it and be able to use it unless you move it to a directory where MiKTeX bothers to look. Adjusting for your paths, open the directory:
C:\Program Files\R\R-3.0.2\share\texmf\tex\latex
and copy the files Sweave.sty
and Rd.sty
to:
C:\MiKTeX 2.9\tex\latex\base
Once the files have been copied you need to update the LaTeX library. This is
easy. Just open a terminal (cmd) window and run texhash
.
The R documentation LaTeX stylesheets require a few packages in order to work.
Open the MiKTeX package manager, e.g. by opening a terminal (cmd) window and
running mpm
, and install the following packages:
upquote
inconsolata
url
mptopdf
fancyvrb
Alternatively MiKTeX can be configured to install missing packages automatically
as needed. Open a terminal (cmd) window and run mo
to open the MiKTeX
Options dialog and set Install missing packages on-the-fly to yes.
For more information on how to install LaTeX packages see MiKTeX instructions.
You should now be able to install R packages from source and build your own packages. However, depending on your configuration this may or may not be enough.
To install the source version of a package from the R package repository,
just define source
as the type:
install.packages("packagename", type="source")
In my case, the R userlib
path was a UNC path. In other words, R
packages were installed to a path that started with two slashes, \\
.
This caused problems as it seems that R cannot build packages to such locations.
The fix is simple once you know it. Again, open Edit environment variables for
your account like above and now add a variable called R_LIBS_USER
with
a value of the path you would like to use for your R libraries. My path is
H:\R\win-library\3.0
If you have already installed packages to your old library path you can move the contents to the new path. You will need to restart R and RStudio if those are open in order for the change to take effect.