R tools for building packages on Windows

This subject is covered in full details in the R installation and Administration Appendix D: The Windows toolset.

Rtools is a collection of software you need to be able to call R from the command line on Windows. You need to be able to call R from the command line to build and install packages from source code. Most packages will also require the program pdflatex.

Install the latest version of R

  • Go to http://cran.stat.sfu.ca, navigate to Windows -> base ->  Download R 3.1.1 for Windows to download the setup program. Locate the setup program on your computer, double-click to launch and follow the instructions. I accepted all defaults.

Install Rtools

  • Go to  http://cran.stat.sfu.ca/bin/windows/Rtools and download the latest version of the Rtools setup program (currently Rtools31.exe).
  • Install Rtools by double-clicking the icon for the setup program you just downloaded
    • At the first three screens of the installer ("Welcome", "Information", "Select Destination"), page just click "Next".
    • At the "Select Components" page choose the default “Package authoring installation” and click "Next".
    • At the "Select Additional Tasks" page find choose to let the installer "Edit the system PATH" by clicking a checkbox that appears beside the current value of the PATH.
    • At the "System Path" page just click "Next"
    • At the "Ready to Install" page click "Install"
  • Add R to your path:
    • Before you start you will need to know the full directory path of the R executable on your system. It should be C:\Program Files\R\R-3.1.1\bin. If not, find the path yourself using the windows file browser to browse your Computer.
    • From the Windows Start icon (bottom left of the computer screen) go to Control Panel -> System -> Advanced system settings -> Environment Variables.
    • In the list of "System variables", find "Path", click to highlight it and then click the "Edit" button.
    • Scroll to the end of the (long) "Variable value" and add a semi-colon and the full path to the R executable. No spaces between the previous entry of the path, your semicolon or the path to the R executable (but preserve any spaces in the path, such as the space between Program and Files in the above example).

Install latex/pdflatex

  • You need to have the "MiKTeX" version of latex installed on your computer. The "TeXLive" version that comes with Cygwin/Xwindows doesn't work, even if you set up your Windows PATH so that the Windows command prompt can see your Cygwin TeX files
  • Download the MiKTeX installer from the MiKTeX site: http://www.miktek.org. The exact location of the downloadable file changes. Look for the Download link in the top toolbar of the MiKTeX homepage.
  • Double-click to run the installer
    • You will be asked a series of questions about the installation. It is OK to accept all default answers. One question is about installation of add-on packages: Do you want to be prompted, or should they be automatically downloaded? If you choose to be prompted, then whenever an add-on is required while using MiKTeX (e.g., when pdflatex'ing a file) you will be prompted to allow the download and to choose the mirror site. It is more convenient to change this default answer to “Yes”.
  • Add MiKTeX to your path. The installer does this automatically, but the placement of MiKTeX in the path may lead to trouble. The R installation documentation suggests that MiKTeX go before the R executable in the path. Don't know why, or if it is critically important, but I followed this instruction. To edit the path:
    • go to the Windows Start icon (bottom left of the computer screen)
    • go to Control Panel -> System -> Advanced system settings -> Environment Variables.
    • In the list of "System variables", find "Path", click to highlight it and then click the "Edit" button. You can now scroll through the very long path variable and make changes.

Use R from the command line

Now you can use R from the Command Prompt to check, build and install your R package:

  • Start the Command Prompt:
    • Click the Windows Start icon and then click All Programs -> Accessories -> Command Prompt
  • change directories to the parent directory of the source code for the R package you want to install
    • For example, if the package is on your desktop, cd C:\Users\<yourname>\Desktop
  • Start R by typing R at the command prompt
    • Use install.packages(“<pkgdir>”,type=”source”) to install, where <pkgdir> is the directory where the package source resides or install.packages(“<pkg>.tar.gz”,type=”source”), where <pkg>.tar.gz is a gzipped tar file that contains the package source.
  • If you want to build an R package to distribute as a binary for other Windows users, use R CMD INSTALL
    • Exit R to return to the Command Prompt.
    • Type   R CMD INSTALL -- build <pkg>.tar.gz. You will now have a file <pkg>.zip that you can send to others. The instructions for others to install is:
    • Download the file to a known directory. Start R and change the working directory to the directory that contains the package. Type install.packages(“<pkg>.zip”,repos=NULL).