Trouble installing rstan on Windows

I’m new to this so please do bear in mind I won’t understand any replies couched in technical language…

I am trying to install RStan using the instructions at:

Windows 10 x64, R 3.6.0 (at: \Documents\R\R-3.6.0) and rtools40 installed (C:\rtools40)
I have updated from R 3.5.2 and installed and then uninstalled rtools35 in the course of following numerous threads here offering solutions.

After downloading packages from CRAN I got various error messages about failure to find \Rtools\mingw_64, however at least pkgbuild::has_build_tools(debug = TRUE) always returned something. Now however,
pkgbuild::has_build_tools(debug = TRUE)
returns:

Scanning R CMD config CC…
cc_path: c:/Rtools/mingw_64/bin/gcc
‘c:/Rtools/mingw_64/bin/gcc’ does not exist
Scanning path…
ls: C:\rtools40\usr\bin\ls.exe
gcc_path:
‘C:/rtools40/usr/mingw_64/bin/gcc.exe’ does not exist
Scanning registry…
Found C:/rtools40 for 4.0
WARNING: Rtools is required to build R packages, but the version of Rtools previously installed in C:/rtools40 has been deleted.

Sys.which(“g++”)
returns:
“”
…although g++.exe is in folder: C:\rtools40\mingw64\bin

writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”)))
returns:
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:

(although I do now have a Makevars.win file, whatever that is, in Documents.R with contents:
CXX14=(BINPREF)g++ CXX14FLAGS=-O3 -march=native -mtune=native CXX11FLAGS=-O3 -march=native -mtune=native CXX14FLAGS=-O3 -march=corei7 -mtune=corei7 CXX14 = (BINPREF)g++ -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

…and adding the line:
CXX = “C:\Rtools40\mingw_64\bin\g++”
…to this doc doesn’t help me.

Probably needless to add that:
devtools::session_info(“rstan”)
returns:
Error in loadNamespace(name) : there is no package called ‘devtools’

What a way to spend a Saturday. Just as well I’m in lockdown!

What does Sys.getenv("BINPREF") return?

Can you also remove everything from your Makevars.win file, except for:

CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations

And try again?

Hi there, thanks for your advice,

Sys.getenv(“BINPREF”) returns:
[1] “”

I changed the Makevars file but same results…

Can you run:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

Then restart R and see what this returns:

Sys.which("make")

Sure…returns:
“C:\rtools40\usr\bin\make.exe”

Great! That should have it fixed, I think

Thanks!..however although
pkgbuild::has_build_tools(debug = TRUE)
returns:
ls: C:\rtools40\usr\bin\ls.exe
gcc_path:
‘C:/rtools40/usr/mingw_64/bin/gcc.exe’ does not exist
Scanning registry…
Found C:/rtools40 for 4.0

…package installation still fails with error messages such as:

  • installing source package ‘colorspace’ …
    ** package ‘colorspace’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
c:/Rtools/mingw_32/bin/gcc -I"C:/Users/MSmith/DOCUME~1/R/R-36~1.0/include" -DNDEBUG -O3 -Wall -std=gnu99 -mtune=generic -c colorspace.c -o colorspace.o
c:/Rtools/mingw_32/bin/gcc -I"C:/Users/MSmith/DOCUME~1/R/R-36~1.0/include" -DNDEBUG -O3 -Wall -std=gnu99 -mtune=generic -c init.c -o init.o
sh: c:/Rtools/mingw_32/bin/gcc: No such file or directory
make: *** [C:/Users/MSmith/DOCUME~1/R/R-36~1.0/etc/i386/Makeconf:208: colorspace.o] Error 127
make: *** Waiting for unfinished jobs…
sh: c:/Rtools/mingw_32/bin/gcc: No such file or directory
make: *** [C:/Users/MSmith/DOCUME~1/R/R-36~1.0/etc/i386/Makeconf:208: init.o] Error 127
ERROR: compilation failed for package ‘colorspace’

  • removing ‘C:/Users/MSmith/Documents/R/R-3.6.0/library/colorspace’

…so still looking for RTools 35 perhaps??

I think you need the development version of pkgbuild for this to work:

remotes::install_github("r-lib/pkgbuild")

OK thanks… what I’m getting is

remotes::install_github(“r-lib/pkgbuild”)
Error in loadNamespace(name) : there is no package called ‘remotes’

That just means you need to install the remotes package:

install.packages("remotes")

That looks a bit better…now I’m getting:

remotes::install_github(“r-lib/pkgbuild”)
Downloading GitHub repo r-lib/pkgbuild@master
WARNING: Rtools is required to build R packages, but the version of Rtools previously installed in C:/rtools40 has been deleted.

Please download and install Rtools custom from RTools: Toolchains for building R and R packages from source on Windows.

checking for file ‘C:\Users\MSmith\AppData\Local\Temp\RtmpSklIFX\remotes1d446d456f7c\r-lib-pkgbuild-c61bb80/DESCRIPTION’ …

√ checking for file ‘C:\Users\MSmith\AppData\Local\Temp\RtmpSklIFX\remotes1d446d456f7c\r-lib-pkgbuild-c61bb80/DESCRIPTION’

  • preparing ‘pkgbuild’:
    checking DESCRIPTION meta-information …
    checking DESCRIPTION meta-information …
    √ checking DESCRIPTION meta-information

  • checking for LF line-endings in source and make files and shell scripts (432ms)

  • checking for empty or unneeded directories

  • building ‘pkgbuild_1.0.6.9000.tar.gz’

  • installing source package ‘pkgbuild’ …
    ** using staged installation
    ** R
    ** byte-compile and prepare package for lazy loading
    ** help
    *** installing help indices
    converting help for package ‘pkgbuild’
    finding HTML links … done
    build html
    clean_dll html
    compile_dll html
    compiler_flags html
    has_build_tools html
    has_compiler html
    has_latex html
    has_rtools html
    needs_compile html
    pkg_has_src html
    pkg_links_to_rcpp html
    pkgbuild_process html
    rcmd_build_tools html
    Rd warning: C:/Users/MSmith/AppData/Local/Temp/Rtmpe8vl3h/R.INSTALL258ebd7175/pkgbuild/man/rcmd_build_tools.Rd:14: file link ‘rcmd_safe_env’ in package ‘callr’ does not exist and so has been treated as a topic
    with_debug html
    without_compiler html
    ** building package indices
    ** testing if installed package can be loaded from temporary location
    *** arch - i386
    *** arch - x64
    ** testing if installed package can be loaded from final location
    *** arch - i386
    *** arch - x64
    ** testing if installed package keeps a record of temporary installation path
  • DONE (pkgbuild)

…maybe I need to resinstall RTools??

That’s fine, the package has been installed. What happens when you run:

pkgbuild::has_build_tools(debug = TRUE)

Hi, here are the results:

pkgbuild::has_build_tools(debug = TRUE)
Scanning R CMD config CC…
cc_path: c:/Rtools/mingw_64/bin/gcc
‘c:/Rtools/mingw_64/bin/gcc’ does not exist
Scanning path…
ls: C:\rtools40\usr\bin\ls.exe
gcc_path:
‘C:/rtools40/usr/mingw_64/bin/gcc.exe’ does not exist
Scanning registry…
Found C:/rtools40 for 4.0
WARNING: Rtools is required to build R packages, but the version of Rtools previously installed in C:/rtools40 has been deleted.

Please download and install Rtools custom from RTools: Toolchains for building R and R packages from source on Windows.

Then I’m afraid I’m stumped. Try reinstalling rtools and see if that does anything.

Otherwise, @bgoodri might be able to help here

I’ll have a go! Many thanks for your help anyway…

It looks like you need R 4.0 to go with rtools40 (and vice versa).

That works! Thank you.

It looks I’m still not quite there…it seems rlang should have been installed but wasn’t.

I’ve been trying to add it and getting a message suggesting it’s looking for Rtools35:

There is a binary version available but the source version is later:
binary source needs_compilation
rlang 0.4.5 0.4.6 TRUE

installing the source package ‘rlang’

trying URL ‘https://cran.ma.imperial.ac.uk/src/contrib/rlang_0.4.6.tar.gz
Content type ‘application/x-gzip’ length 835198 bytes (815 KB)
downloaded 815 KB

  • installing source package ‘rlang’ …
    ** package ‘rlang’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
c:/Rtools/mingw_32/bin/gcc -I"C:/Users/MSmith/DOCUME~1/R/R-36~1.0/include" -DNDEBUG -I./lib/ -O3 -Wall -std=gnu99 -mtune=generic -c capture.c -o capture.o
sh: c:/Rtools/mingw_32/bin/gcc: No such file or directory
make: *** [C:/Users/MSmith/DOCUME~1/R/R-36~1.0/etc/i386/Makeconf:208: capture.o] Error 127
ERROR: compilation failed for package ‘rlang’

  • removing ‘C:/Users/MSmith/Documents/R/R-3.6.0/library/rlang’

The downloaded source packages are in
‘C:\Users\MSmith\AppData\Local\Temp\RtmpsxrHfc\downloaded_packages’
Warning message:
In install.packages(“rlang”) :
installation of package ‘rlang’ had non-zero exit status

(should I start another topic on this subject?)

Choose “No” when it asks you if you want to install rlang from source.

That seems to work. The next stage, for me, was to try and install R McElreath’s Rethinking.
install.packages(c(“coda”,“mvtnorm”,“devtools”,“loo”,“dagitty”))
…worked

library(devtools)
…worked with warnings:
Loading required package: usethis
Warning messages:
1: package ‘devtools’ was built under R version 3.6.3
2: package ‘usethis’ was built under R version 3.6.3

devtools::install_github(“rmcelreath/rethinking”)
…didn’t work,
WARNING: Rtools is required to build R packages, but no version of Rtools compatible with R 3.6.0 was found. (Only the following incompatible version(s) of Rtools were found:4.0)

Please download and install Rtools custom from (etc)
Downloading GitHub repo rmcelreath/rethinking@master
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All
2: CRAN packages only
3: None
4: rlang (0.4.5 -> 0.4.6) [CRAN]

…I tried carrying on (entering 1) but of course:
Error: Failed to install ‘rethinking’ from GitHub:
(converted from warning) cannot remove prior installation of package ‘rlang’