R crashes when trying out example(stan_model...)

Hello,

I have tried to install and use rstan on my Windows 10 computer. I am using RStudio Version 1.4.1106 with R version 4.1.0. I was following the description on GitHub (RStan Getting Started · stan-dev/rstan Wiki · GitHub). However, when I try out the example, R always crashes.

This is what I did. First, I removed rstan and cleared RData.

remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")

Then, I closed RStudio and opened it up again to install rstan with the following command, producing the following output:

> install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)

Installing package into ‘C:/Users/Oswin/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.1/rstan_2.21.5.zip'
Content type 'application/zip' length 5268817 bytes (5.0 MB)
downloaded 5.0 MB

package ‘rstan’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\Oswin\AppData\Local\Temp\RtmpQPqyZr\downloaded_packages

To try out my installation, I used:

example(stan_model, package = "rstan", run.dontrun = TRUE)

This runs for a bit and then R crashes:

Does anyone have an idea what is going wrong here?

Sorry for the delay in getting to this! I just need to check some config details to see where the issue might be coming from.

Can you post the outputs from the following R commands:

readLines("~/.Renviron")
readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")
Sys.getenv("PATH")

One more: Sys.getenv("RTOOLS40_HOME")

Hey,

Thanks for your replies. I had to adapt the paths a bit, I hope these are the files that you were looking for:


> readLines("C:/Program Files/R/R-4.1.0/src/library/windlgs/src/Makevars.win")
[1] "PKG_LIBS=$(GRAPHAPP_LIB)"
> readLines("C:/Program Files/R/R-4.1.0/library/base/help/Renviron.html")
[1] "<html><head><meta http-equiv='refresh' content='0; url=../html/Startup.html'></head></html>"
> readLines("C:/Program Files/R/R-4.1.0/library/base/R/Rprofile")
  [1] "### This is the system Rprofile file. It is always run on startup."                                                           
  [2] "### Additional commands can be placed in site or user Rprofile files"                                                         
  [3] "### (see ?Rprofile)."                                                                                                         
  [4] ""                                                                                                                             
  [5] "### Copyright (C) 1995-2020 The R Core Team"                                                                                  
  [6] ""                                                                                                                             
  [7] "### Notice that it is a bad idea to use this file as a template for"                                                          
  [8] "### personal startup files, since things will be executed twice and in"                                                       
  [9] "### the wrong environment (user profiles are run in .GlobalEnv)."                                                             
 [10] ""                                                                                                                             
 [11] ".GlobalEnv <- globalenv()"                                                                                                    
 [12] "attach(NULL, name = \"Autoloads\")"                                                                                           
 [13] ".AutoloadEnv <- as.environment(2)"                                                                                            
 [14] "assign(\".Autoloaded\", NULL, envir = .AutoloadEnv)"                                                                          
 [15] "T <- TRUE"                                                                                                                    
 [16] "F <- FALSE"                                                                                                                   
 [17] "R.version <- structure(R.Version(), class = \"simple.list\")"                                                                 
 [18] "version <- R.version            # for S compatibility"                                                                        
 [19] ""                                                                                                                             
 [20] "## for backwards compatibility only"                                                                                          
 [21] "R.version.string <- R.version$version.string"                                                                                 
 [22] ""                                                                                                                             
 [23] "## NOTA BENE: options() for non-base package functionality are in places like"                                                
 [24] "##            --------- ../utils/R/zzz.R"                                                                                     
 [25] ""                                                                                                                             
 [26] "options(keep.source = interactive())"                                                                                         
 [27] "options(warn = 0)"                                                                                                            
 [28] "# options(repos = c(CRAN=\"@CRAN@\"))"                                                                                        
 [29] "# options(BIOC = \"http://www.bioconductor.org\")"                                                                            
 [30] ""                                                                                                                             
 [31] "## setting from an env variable added in 4.0.2"                                                                               
 [32] "local({to <- as.integer(Sys.getenv(\"R_DEFAULT_INTERNET_TIMEOUT\", 60))"                                                      
 [33] "    if (is.na(to) || to <= 0) to <- 60L"                                                                                      
 [34] "    options(timeout = to)"                                                                                                    
 [35] "})"                                                                                                                           
 [36] "options(encoding = \"native.enc\")"                                                                                           
 [37] "options(show.error.messages = TRUE)"                                                                                          
 [38] "## keep in sync with PrintDefaults() in  ../../main/print.c :"                                                                
 [39] "options(scipen = 0)"                                                                                                          
 [40] "options(max.print = 99999)# max. #{entries} in internal printMatrix()"                                                        
 [41] "options(add.smooth = TRUE)# currently only used in 'plot.lm'"                                                                 
 [42] ""                                                                                                                             
 [43] "if(isFALSE(as.logical(Sys.getenv(\"_R_OPTIONS_STRINGS_AS_FACTORS_\","                                                         
 [44] "                                 \"FALSE\")))) {"                                                                             
 [45] "    options(stringsAsFactors = FALSE)"                                                                                        
 [46] "} else {"                                                                                                                     
 [47] "    options(stringsAsFactors = TRUE)"                                                                                         
 [48] "}"                                                                                                                            
 [49] ""                                                                                                                             
 [50] "if(!interactive() && is.null(getOption(\"showErrorCalls\")))"                                                                 
 [51] "    options(showErrorCalls = TRUE)"                                                                                           
 [52] ""                                                                                                                             
 [53] "local({dp <- Sys.getenv(\"R_DEFAULT_PACKAGES\")"                                                                              
 [54] "       if(identical(dp, \"\")) ## it fact methods is done first"                                                              
 [55] "           dp <- c(\"datasets\", \"utils\", \"grDevices\", \"graphics\","                                                     
 [56] "                   \"stats\", \"methods\")"                                                                                   
 [57] "       else if(identical(dp, \"NULL\")) dp <- character(0)"                                                                   
 [58] "       else dp <- strsplit(dp, \",\")[[1]]"                                                                                   
 [59] "       dp <- sub(\"[[:blank:]]*([[:alnum:]]+)\", \"\\\\1\", dp) # strip whitespace"                                           
 [60] "       options(defaultPackages = dp)"                                                                                         
 [61] "    })"                                                                                                                       
 [62] ""                                                                                                                             
 [63] "## Expand R_LIBS_* environment variables."                                                                                    
 [64] "Sys.setenv(R_LIBS_SITE ="                                                                                                     
 [65] "           .expand_R_libs_env_var(Sys.getenv(\"R_LIBS_SITE\")))"                                                              
 [66] "Sys.setenv(R_LIBS_USER ="                                                                                                     
 [67] "           .expand_R_libs_env_var(Sys.getenv(\"R_LIBS_USER\")))"                                                              
 [68] ""                                                                                                                             
 [69] "local({"                                                                                                                      
 [70] "    if(nzchar(tl <- Sys.getenv(\"R_SESSION_TIME_LIMIT_CPU\")))"                                                               
 [71] "        setSessionTimeLimit(cpu = tl)"                                                                                        
 [72] "    if(nzchar(tl <- Sys.getenv(\"R_SESSION_TIME_LIMIT_ELAPSED\")))"                                                           
 [73] "        setSessionTimeLimit(elapsed = tl)"                                                                                    
 [74] "})"                                                                                                                           
 [75] ""                                                                                                                             
 [76] ".First.sys <- function()"                                                                                                     
 [77] "{"                                                                                                                            
 [78] "    for(pkg in getOption(\"defaultPackages\")) {"                                                                             
 [79] "        res <- require(pkg, quietly = TRUE, warn.conflicts = FALSE,"                                                          
 [80] "                       character.only = TRUE)"                                                                                
 [81] "        if(!res)"                                                                                                             
 [82] "            warning(gettextf('package %s in options(\"defaultPackages\") was not found', sQuote(pkg)),"                       
 [83] "                    call. = FALSE, domain = NA)"                                                                              
 [84] "    }"                                                                                                                        
 [85] "}"                                                                                                                            
 [86] ""                                                                                                                             
 [87] "## called at C level in the startup process prior to .First.sys"                                                              
 [88] ".OptRequireMethods <- function()"                                                                                             
 [89] "{"                                                                                                                            
 [90] "    pkg <- \"methods\" # done this way to avoid R CMD check warning"                                                          
 [91] "    if(pkg %in% getOption(\"defaultPackages\"))"                                                                              
 [92] "        if(!require(pkg, quietly = TRUE, warn.conflicts = FALSE,"                                                             
 [93] "                    character.only = TRUE))"                                                                                  
 [94] "            warning('package \"methods\" in options(\"defaultPackages\") was not found',"                                     
 [95] "                    call. = FALSE)"                                                                                           
 [96] "}"                                                                                                                            
 [97] ""                                                                                                                             
 [98] "if(nzchar(Sys.getenv(\"R_BATCH\"))) {"                                                                                        
 [99] "    .Last.sys <- function()"                                                                                                  
[100] "    {"                                                                                                                        
[101] "        cat(\"> proc.time()\\n\")"                                                                                            
[102] "        print(proc.time())"                                                                                                   
[103] "    }"                                                                                                                        
[104] "    ## avoid passing on to spawned R processes"                                                                               
[105] "    ## A system has been reported without Sys.unsetenv, so try this"                                                          
[106] "    try(Sys.setenv(R_BATCH=\"\"))"                                                                                            
[107] "}"                                                                                                                            
[108] ""                                                                                                                             
[109] "local({"                                                                                                                      
[110] "    if(nzchar(rv <- Sys.getenv(\"_R_RNG_VERSION_\")))"                                                                        
[111] "        suppressWarnings(RNGversion(rv))"                                                                                     
[112] "})"                                                                                                                           
[113] ""                                                                                                                             
[114] ".sys.timezone <- NA_character_"                                                                                               
[115] "###-*- R -*-"                                                                                                                 
[116] ""                                                                                                                             
[117] "## this will break if R is on a network share"                                                                                
[118] ".Library <- file.path(chartr(\"\\\\\", \"/\", R.home()), \"library\")"                                                        
[119] ".Library.site <- Sys.getenv(\"R_LIBS_SITE\")"                                                                                 
[120] ".Library.site <- if(!nzchar(.Library.site)) file.path(R.home(), \"site-library\") else unlist(strsplit(.Library.site, \";\"))"
[121] ".Library.site <- .Library.site[file.exists(.Library.site)]"                                                                   
[122] ""                                                                                                                             
[123] "if(!nzchar(Sys.getenv(\"R_LIBS_USER\")))"                                                                                     
[124] "    Sys.setenv(R_LIBS_USER="                                                                                                  
[125] "               file.path(Sys.getenv(\"R_USER\"), \"R\","                                                                      
[126] "                         \"win-library\","                                                                                    
[127] "                         paste(R.version$major,"                                                                              
[128] "                               sub(\"\\\\..*$\", \"\", R.version$minor),"                                                     
[129] "                               sep=\".\")"                                                                                    
[130] "                         ))"                                                                                                  
[131] ""                                                                                                                             
[132] "invisible(.libPaths(c(unlist(strsplit(Sys.getenv(\"R_LIBS\"), \";\")),"                                                       
[133] "                      unlist(strsplit(Sys.getenv(\"R_LIBS_USER\"), \";\"))"                                                   
[134] "                      )))"                                                                                                    
[135] ""                                                                                                                             
[136] "local({"                                                                                                                      
[137] "    popath <- Sys.getenv(\"R_TRANSLATIONS\", \"\")"                                                                           
[138] "    if(!nzchar(popath)) {"                                                                                                    
[139] "        paths <- file.path(.libPaths(), \"translations\", \"DESCRIPTION\")"                                                   
[140] "        popath <- dirname(paths[file.exists(paths)][1])"                                                                      
[141] "    }"                                                                                                                        
[142] "    bindtextdomain(\"R\", popath)"                                                                                            
[143] "    bindtextdomain(\"R-base\", popath)"                                                                                       
[144] "    bindtextdomain(\"RGui\", popath)"                                                                                         
[145] "    assign(\".popath\", popath, .BaseNamespaceEnv)"                                                                           
[146] "})"                                                                                                                           
[147] ""                                                                                                                             
[148] "if(nzchar(Sys.getenv(\"R_PAPERSIZE\"))) {"                                                                                    
[149] "    options(papersize = Sys.getenv(\"R_PAPERSIZE\"))"                                                                         
[150] "} else {"                                                                                                                     
[151] "    if(grepl(\"(canada|united.states)\", Sys.getlocale(\"LC_MONETARY\"),"                                                     
[152] "             ignore.case = TRUE)) options(papersize = \"letter\")"                                                            
[153] "    else options(papersize = \"a4\")"                                                                                         
[154] "}"                                                                                                                            
[155] ""                                                                                                                             
[156] "options(pager = if(length(grep(\"--ess\", commandArgs()))) \"console\" else \"internal\","                                    
[157] "        useFancyQuotes = (.Platform$GUI == \"Rgui\"),"                                                                        
[158] "        pdfviewer = Sys.getenv(\"R_PDFVIEWER\", file.path(R.home(\"bin\"), \"open.exe\")))"                                   
[159] ""                                                                                                                             
[160] "if(.Platform$GUI == \"Rgui\")"                                                                                                
[161] "    Sys.setenv(GFORTRAN_STDOUT_UNIT = \"-1\", GFORTRAN_STDERR_UNIT = \"-1\")"                                                 
[162] ""                                                                                                                             
[163] "local({"                                                                                                                      
[164] "    br <- Sys.getenv(\"R_BROWSER\", NA_character_)"                                                                           
[165] "    if(!is.na(br)) options(browser = br)"                                                                                     
[166] "    tests_startup <- Sys.getenv(\"R_TESTS\")"                                                                                 
[167] "    if(nzchar(tests_startup)) source(tests_startup)"                                                                          
[168] "    ca_cert <- Sys.getenv(\"CURL_CA_BUNDLE\", NA_character_)"                                                                 
[169] "    if(!is.na(ca_cert) &&"                                                                                                    
[170] "       file.exists(ca_path <- file.path(R.home(\"etc\"), \"curl-ca-bundle.crt\")))"                                           
[171] "        Sys.setenv(CURL_CA_BUNDLE = ca_path)"                                                                                 
[172] "})"                                                                                                                           
> readLines("C:/Program Files/R/R-4.1.0/src/library/windlgs/src/Makevars.win")
[1] "PKG_LIBS=$(GRAPHAPP_LIB)"
> Sys.getenv("PATH")
[1] "C:\\Program Files\\R\\R-4.1.0\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\MATLAB\\R2020b\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Julia-1.6.1\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39\\Scripts;C:\\Program Files\\dotnet\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Users\\Oswin\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\Scripts;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\site-packages;C:\\Users\\Oswin\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\;C:\\Program Files\\Inkscape\\bin;;C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021.1.2\\bin;"
> Sys.getenv("RTOOLS40_HOME")
[1] "C:\\Program Files\\rtools40"

Ah no, I needed you to run the code as-is without adapting any paths

Even if it returns an error that no file exists, that’s also helpful for me to know

Oh, okay, sorry, all the readLines give me errors then:

> readLines("~/.Renviron")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Oswin/Documents/.Renviron': No such file or directory
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Oswin/Documents/.Rprofile': No such file or directory
> readLines("~/.R/Makevars.win")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Oswin/Documents/.R/Makevars.win': No such file or directory
> Sys.getenv("PATH")
[1] "C:\\Program Files\\R\\R-4.1.0\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\MATLAB\\R2020b\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Julia-1.6.1\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39\\Scripts;C:\\Program Files\\dotnet\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Users\\Oswin\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\Scripts;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\site-packages;C:\\Users\\Oswin\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\;C:\\Program Files\\Inkscape\\bin;;C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021.1.2\\bin;"
> Sys.getenv("RTOOLS40_HOME")
[1] "C:\\Program Files\\rtools40"

Ah it looks like you’ve missed a step when installing RTools. Can you complete the installation steps at the RTools4 download page: Using Rtools4 on Windows, specifically the " Putting Rtools on the PATH" section

I have done the “Putting Rtools on the PATH” section, however, this only changes the Renviron one and not the Rprofile or the Makevars.win output:

> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Oswin/Documents/.Rprofile': No such file or directory
> readLines("~/.R/Makevars.win")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/Oswin/Documents/.R/Makevars.win': No such file or directory
> Sys.getenv("PATH")
[1] "C:\\Program Files\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.1.0\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\MATLAB\\R2020b\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Julia-1.6.1\\bin;C:\\Users\\Oswin\\AppData\\Local\\Programs\\Python\\Python39\\Scripts;C:\\Program Files\\dotnet\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Users\\Oswin\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\Scripts;C:\\Users\\Oswin\\AppData\\Roaming\\Python\\Python38\\site-packages;C:\\Users\\Oswin\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\;C:\\Program Files\\Inkscape\\bin;;C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021.1.2\\bin;"
> Sys.getenv("RTOOLS40_HOME")
[1] "C:\\Program Files\\rtools40"

Yep that’s fine, can you try the example model again?

I completely restarted R, then I used:

> example(stan_model, package = "rstan", run.dontrun = TRUE)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.5, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file

It still crashed at the same spot.

The next step is to try reinstalling rstan and StanHeaders from source.

Can you restart R and then run:

remove.packages(c("StanHeaders", "rstan"))
remove.packages(c("StanHeaders", "rstan"), type = "source")

Then if the install completes successfully, try the example model again

Yes, now it works. Thanks so much for your help!

1 Like