Error in building CmdStanr on Windows 10

Hi all ,
@jonah @rok_cesnovar
I am just trying to start working with cmdstanr. I used this link to install it and then following.

install.packages(“cmdstanr”, repos = c(“https://mc-stan.org/r-packages/”, getOption(“repos”)))
library(cmdstanr) library(posterior) library(bayesplot) color_scheme_set(“brightblue”)

The downloaded source packages are in
‘C:\Users\nanayaer\AppData\Local\Temp\RtmpQ1twyS\downloaded_packages’

library(cmdstanr)
library(posterior)
library(bayesplot)
color_scheme_set(“brightblue”)

But, this command didn’t work for me.

install_cmdstan(cores = 2)

  • Latest CmdStan release is v2.24.1
  • Installing CmdStan v2.24.1 in C:/Users/nanayaer/Documents/.cmdstanr/cmdstan-2.24.1
  • Downloading cmdstan-2.24.1.tar.gz from GitHub…
  • Download complete
  • Unpacking archive…
    Error in mydir.create(name) :
    failed to create directory ‘cmdstan-2.24.1/stan/lib/stan_math/lib/boost_1.72.0/tools/auto_index/doc/html/boost_autoindex/tut/con’
    In addition: Warning message:
    In dir.create(path, showWarnings = TRUE, recursive = TRUE, …) :
    cannot create dir ‘cmdstan-2.24.1\stan\lib\stan_math\lib\boost_1.72.0\tools\auto_index\doc\html\boost_autoindex\tut\con’, reason 'Invalid argument

Could you please help me?

Hi,

are you using R4.0 with Rtools 4.0? If so, can you run:

check_rtools40_windows_toolchain <- function(fix = FALSE, quiet = FALSE) {
  rtools_path <- Sys.getenv("RTOOLS40_HOME")
  # If RTOOLS40_HOME is not set (the env. variable gets set on install)
  # we assume that RTools 40 is not installed.
  if (!nzchar(rtools_path)) {
    stop(
      "\nRTools 4.0 was not found but is required to run CmdStan with R version 4.x.",
      "\nPlease install RTools 4.0 and run check_cmdstan_toolchain().",
      call. = FALSE
    )
  }
  # If RTools is installed in a path with spaces or brackets
  # we error as this path is not valid
  if (regexpr("\\(|)| ", rtools_path) > 0) {
    stop(
      "\nRTools 4.0 is installed in a path with spaces or brackets, which is not supported.",
      "\nPlease reinstall RTools 4.0 to a valid path, restart R, and then run check_cmdstan_toolchain().",
      call. = FALSE
    )
  }
  toolchain_path <- cmdstanr:::repair_path(file.path(rtools_path, "mingw64", "bin"))
  mingw32_make_path <- dirname(Sys.which("mingw32-make"))
  gpp_path <- dirname(Sys.which("g++"))
  if (!nzchar(mingw32_make_path) || !nzchar(gpp_path)) {
    if (!fix) {
      stop(
        "\nRTools installation found but PATH was not properly set.",
        "\nRun check_cmdstan_toolchain(fix = TRUE) to fix the issue.",
        call. = FALSE
      )
    } else {
      if (!quiet) message("Installing mingw32-make and writing RTools path to ~/.Renviron ...")
      install_mingw32_make()
      write('PATH="${RTOOLS40_HOME}\\usr\\bin;${RTOOLS40_HOME}\\mingw64\\bin;${PATH}"', file = "~/.Renviron", append = TRUE)
      Sys.setenv(PATH = paste0(Sys.getenv("RTOOLS40_HOME"), "\\usr\\bin;", Sys.getenv("RTOOLS40_HOME"), "\\mingw64\\bin;", Sys.getenv("PATH")))
	  check_rtools40_windows_toolchain(fix = FALSE, quiet = quiet)
	  return(invisible(NULL))
    }
  }
  # Check if the mingw32-make and g++ get picked up by default are the RTools-supplied ones
  if (toolchain_path != mingw32_make_path || gpp_path != toolchain_path) {
    if (!fix) {
      stop(
        "\nOther C++ toolchains installed on your system conflict with RTools.",
        "\nPlease run check_cmdstan_toolchain(fix = TRUE) to fix the issue.",
        call. = FALSE
      )
    } else {
      if (!quiet) message("Installing mingw32-make and writing RTools path to ~/.Renviron ...")
      install_mingw32_make()
      write('PATH="${RTOOLS40_HOME}\\usr\\bin;${RTOOLS40_HOME}\\mingw64\\bin;${PATH}"', file = "~/.Renviron", append = TRUE)
      Sys.setenv(PATH = paste0(Sys.getenv("RTOOLS40_HOME"), "\\usr\\bin;", Sys.getenv("RTOOLS40_HOME"), "\\mingw64\\bin;", Sys.getenv("PATH")))
	  check_rtools40_windows_toolchain(fix = FALSE, quiet = quiet)
	  return(invisible(NULL))
    }
  }
}

Hi Rok,

Yes I am using R 4.0.2 and Rtools 4.0
Platform: x86_64-w64-mingw32/x64 (64-bit)

I think it is required to just run this code. I done it.

Thank you

Ok sorry, I should have added to run
check_rtools40_windows_toolchain()

Hi, It’s okay.

It gave me an error

Error in repair_path(file.path(rtools_path, “mingw64”, “bin”)) :
could not find function “repair_path”

Oh sorry. Please try again, I updated the example above in my post.

Please see this error

Error: ‘.repair_path’ is not an exported object from ‘namespace:cmdstanr’

In addition: Warning message: Can’t find CmdStan makefile to detect version number. Path may not point to valid installation.

Ah, missed a semicolon. Try again please. We will get to " Can’t find CmdStan makefile to detect version number" once this first issue is fixed.

Hi Rok,

This what I have got after running the above code

check_rtools40_windows_toolchain()
Error:
RTools installation found but PATH was not properly set.
Run check_cmdstan_toolchain(fix = TRUE) to fix the issue.

Then I run,

check_cmdstan_toolchain(fix = TRUE)
Error in check_cmdstan_toolchain(fix = TRUE) :
could not find function “check_cmdstan_toolchain”

Is it?

Hey, instead of check_cmdstan_toolchain() run check_rtools40_windows_toolchain(fix = TRUE).

The check_cmdstan_toolchain has not been exposed yet. Sorry for the errors, this function is in development right now and will be part of cmdstanr in the very near future so we can avoid the issue you are having.

Hi Rok, Thank you for the help,

But, I failed again and gave me this error.

Installing mingw32-make and writing RTools path to ~/.Renviron …
Error in install_mingw32_make() :
could not find function “install_mingw32_make”

I am following this link to install cmdstanr

You did nothing wrong. This code I pasted is on a branch I am building.
This is the missing function:

install_mingw32_make <- function() {
processx::run(
“pacman”,
args = c("-Syu", “mingw-w64-x86_64-make”,"–noconfirm"),
wd = file.path(Sys.getenv(“RTOOLS40_HOME”), “usr”, “bin”),
error_on_status = TRUE
)
invisible(NULL)
}

Is it missing something? I was unable to run it properly.
I mean, should I run check_rtools40_windows_toolchain(fix = TRUE) along with above code?

Run the below code so that the install_mingw32_make() function exists and then rerun check_rtools40_windows_toolchain(fix = TRUE)

install_mingw32_make <- function() {
processx::run(
"pacman",
args = c("-Syu", "mingw-w64-x86_64-make","–noconfirm"),
wd = file.path(Sys.getenv("RTOOLS40_HOME"), "usr", "bin"),
error_on_status = TRUE
)
invisible(NULL)
}

I don’t know I am asking a stupid question here, Please see below errors

install_mingw32_make <- function() {
+ processx::run(
+ “pacman”,
Error: unexpected input in:
" processx::run(
“"

args = c("-Syu", “mingw-w64-x86_64-make”,"–noconfirm"),

Error: unexpected input in " args = c("-Syu", “"

wd = file.path(Sys.getenv(“RTOOLS40_HOME”), “usr”, “bin”),

Error: unexpected input in " wd = file.path(Sys.getenv(“"

error_on_status = TRUE

)
Error: unexpected ‘)’ in " )"
invisible(NULL)
}
Error: unexpected ‘}’ in “}”

So, I guess it is a typing errror.

I think its the formatting of quote symbols that is done on the forums. I edited the post. Please try again.

It was a my error, because I didn’t check quotations.
I could run it and then run,

     check_rtools40_windows_toolchain(fix = TRUE) 

Why is this error occurred?

Installing mingw32-make and writing RTools path to ~/.Renviron …
Error in processx::run(“pacman”, args = c("-Syu", “mingw-w64-x86_64-make”, : System command ‘pacman’ failed, exit status: 1, stderr: E> error: target not found: �noconfirm Type .Last.error.trace to see where the error occured

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

It generates

“C:\rtools40”