Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Latest commit

 

History

History
150 lines (89 loc) · 6.96 KB

faq.md

File metadata and controls

150 lines (89 loc) · 6.96 KB

R on Windows FAQ

These are some common issues related to installing R packages on Windows with Rtools. This complements the official R-FAQ from CRAN.

What is Rtools

Rtools is the toolchain bundle that is used on Windows to build R base and R packages that contain compiled code. You can download it from CRAN. The latest builds of rtools4 contain 3 toolchains:

  • C:\rtools40\mingw32: the 32-bit gcc-8-3.0 toolchain used as of R 4.0.0
  • C:\rtools40\mingw64: the 64-bit gcc-8-3.0 toolchain used as of R 4.0.0
  • C:\rtools40\ucrt64: a new 64-bit gcc-10.3.0 toolchain targeting ucrt

You only need Rtools if you want to compile R packages from source that contain C/C++/Fortran. By default, R for Windows installs the precompiled binary packages from CRAN, for which you do not need rtools.

How to install Rtools40:

Please see instructions in: https://github.com/r-windows/docs

Why does Rtools not put itself on the PATH automatically?

Some versions of rtools in the past would automatically alter the global windows system PATH variable and add the rtools path. This was problematic for several reasons:

The main problem is that other Windows programs may also attempt to do this (Strawberry Perl for example) and hence if those programs are installed after rtools, they may mask the rtools utilities from the PATH. But it would also hold vice-versa: prepending the windows system PATH with the rtools utilities could have undesired side-effects for other software relying on the path.

For these reasons, the best way to set the PATH for R is in your ~/.Renviron file.

What is the difference between the msys, mingw32, and mingw64 shells?

These are almost the same, the only difference is the default PATH. The mingw32/mingw64 shell automatically put the respective gcc version on the path, whereas the msys shell has no toolchain on the path. Other than that there is no difference.

Does Rtools40 include git or svn? Why not?

Rtools40 does not include git or svn clients. Most Windows users have Git for Windows or TortoiseSVN installed and configured, so we recommend to use these.

Is Git-for-Windows compatible with rtools40?

Yes. If you have Git for Windows you will be able to use git commands from the rtools40 shell as well. Note that Git for Windows also includes it's own shell called "Git Bash" which looks very similar to Rtools Bash. That is because they use exactly the same system.

Most git commands will work the same in Rtools as Git Bash, however for git commands that use vim for interactive editing things, it is safer to use Git Bash (because it includes a special vim that rtools doesn't have).

How to extract a tarball with symlinks

Windows does not support symlinks. Cygwin has several ways to emulate symlinks, but each has limitations. To extract a tarball which contains symlinks (such as the official R source releases) you can set this environment variable:

MSYS="winsymlinks:lnk"

This will create symlinks as Windows shortcuts with a special header and the R/O attribute set.

How does R find compilers

The most important thing is that the rtools40 make is on the path. It is not needed to put gcc on the path.

# Check your path
Sys.getenv('PATH')

# Confirm that make.exe is on the PATH
Sys.which('make')
## "C:\\rtools40\\usr\\bin\\make.exe"

Once make is available, R will lookup make variables via R CMD config to find the path to gcc and other tools. To test this manually in R, let's lookup the path to the C++11 compiler:

tools::Rcmd(c('config', 'CXX11'))
# C:/Rtools/mingw_64/bin/g++

This system is the same on all operating systems (Windows, MacOS, Linux).

How to test if the compiler is available in R?

The script below looks up the compiler via R CMD config and then tests that it works. The same script can be used on any platform (not just windows).

r_cmd_config <- function(var){
  tools::Rcmd(c("config", var), stdout = TRUE)
}

test_compiler <- function(){
  testprog <- '#include <iostream>\nint main() {std::cout << "Hello World!";}'
  make <- Sys.which('make')
  if(!nchar(make))
    stop("Did not find 'make' on the PATH")
  CXX <- r_cmd_config("CXX")
  src <- tempfile(fileext = '.cpp')
  obj <- sub('cpp$', 'o', src)
  writeLines(testprog, con = src)
  out <- system2('make', c(obj, sprintf('CXX="%s"', CXX)))
  if(!file.exists(obj))
    stop("Failed to compile example program: ", out)
  TRUE
}

How to build base R on Windows

See the r-windows/r-base repository for scripts and documenation for building the R for Windows installer from source.

Does rtools40 include a debugger

Yes, both GDB and drmingw via the package manager. To install the 64-bit version run this in the mingw64 shell:

pacman -Sy mingw-w64-x86_64-gdb

To install 32-bit gdb use:

pacman -Sy mingw-w64-i686-gdb

Same for pacman -Sy mingw-w64-x86_64-drmingw (which easier to use for simple debugging).

See the FAQ 8.4 How do I debug code that I have compiled and dyn.load-ed? to get started with debugging R packages.

How to install rJava on Windows?

The latest CRAN version of rJava will find the jvm.dll automatically, without manually setting the PATH or JAVA_HOME. However note that:

  • To use rJava in 32-bit R, you need Java for Windows x86 (direct download)
  • To use rJava in 64-bit R, you need Java for Windows x64 (direct download)
  • To build or check R packages with multi-arch (the default) you need to install both Java For Windows x64 as well as Java for Windows x86. On Win 64, the former installs in C:\Program files\Java\ and the latter in C:\Program Files (x86)\Java\ so they do not conflict.

As of Java version 9, support for x86 (win32) has been discontinued. Hence the latest working multi-arch setup is to install both jdk-8u251-windows-i586.exe and jdk-8u251-windows-x64.exe and then the binary package from CRAN:

install.packages("rJava")

The binary package from CRAN should pick up on the jvm by itself. Experts only: to build rJava from source, you need the --merge-multiarch flag:

install.packages('rJava', type = 'source', INSTALL_opts='--merge-multiarch')