Rstan incosistently running

Rstan will inconsistently run on my computer. The problem seemed to be about Mac OS X Catalina and Rcpp but there is a new error message today:

"
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from :1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:535:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
#pragma clang diagnostic pop
^
In file included from :1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file173720520784.cpp 2> file173720520784.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection
"

I have:
1. uninstalled rstan, the macOS R toolchain, and checked by:
pkgbuild::has_build_tools(debug = TRUE)
[1] FALSE

  1. reinstalled everything and checked by
    pkgbuild::has_build_tools(debug = TRUE)
    [1] TRUE

  2. changing the header as suggested on another forum

Please any help would be much appreciated!

library("rstan")
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

#make stan file schools.stan####
#####contents of the schools.stan file####
# data {
#   int<lower=0> J;         // number of schools 
#   real y[J];              // estimated treatment effects
#   real<lower=0> sigma[J]; // standard error of effect estimates 
# }
# parameters {
#   real mu;                // population treatment effect
#   real<lower=0> tau;      // standard deviation in treatment effects
#   vector[J] eta;          // unscaled deviation from mu by school
# }
# transformed parameters {
#   vector[J] theta = mu + tau * eta;        // school treatment effects
# }
# model {
#   target += normal_lpdf(eta | 0, 1);       // prior log-density
#   target += normal_lpdf(y | theta, sigma); // log-likelihood
# }
#
########



schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))



fit <- stan(file = 'schools.stan', 
            data = schools_dat)


print(fit)

Operating System:
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Interface Version:

stan_version()
[1] “2.19.1”
Compiler/Toolkit:

Hi,

  1. What does ~/.R/Makevars and ~/.Rprofile contain?
    I have R v4.0.0 and they contain:

Makevars

FC = /usr/local/gfortran/bin/gfortran
FLIBS = -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function 

.Rprofile

## WORKAROUND: https://github.com/rstudio/rstudio/issues/6692
## Revert to 'sequential' setup of PSOCK cluster in RStudio Console on macOS and R 4.0.0
if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && 
    Sys.info()["sysname"] == "Darwin" && getRversion() == "4.0.0") {
  parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
}
  1. Are you using the Xcode compiler toolkit (i.e., have you removed other compilers and installed Xcode and run xcode-select --install in the terminal?
2 Likes

Thank you very much for your help!

  1. ~/.R/Makevars contains:

clang: start

CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include

SHLIB_CXXLDFLAGS+=-Wl,-rpath,{R_HOME}/lib {R_HOME}/lib/libc++abi.1.dylib
SHLIB_CXX14LDFLAGS+=-Wl,-rpath,{R_HOME}/lib {R_HOME}/lib/libc++abi.1.dylib

clang: end

and

~/.Rprofile cannot be found when I search my computer.

  1. I believe I have successfully uninstalled and reinstalled Xcode compilier based on the change between pkgbuild::has_build_tools(debug = TRUE) output changing from TRUE, then after I uninstall the output is FALSE, and when I reinstall it is TRUE. Is this not a reliable way to determine if I have uninstalled/reinstalled properly?

g++ --version:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Hi

  1. Change Makevars to what I have. You don’t need any directories etc. anymore.
  2. Add a file in your home directory called ~/.Rprofile where you add the content I gave you above.
  3. Please do g++ --version in the terminal and EDIT[paste] it to a post here.
3 Likes

When making .Rprofile file, a message appeared and prevented creating the file:
“You can’t use a name that begins with a dot “.”, because these names are reserved for the system. Please choose another name.”

So, what happens if you type touch ~/.Rprofile

Richard

Running touch ~/.Rprofile in the terminal returns a new line. Doesn’t appear to do anything but there were no messages or errors.

Good. Now open the file, edit it, and save it:

open -e ~/.Rprofile

Thank you!

Now when I run my original test code posted for schools.stan the message is:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from :1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:82:
In file included from /usr/local/clang7/include/c++/v1/new:91:
In file included from /usr/local/clang7/include/c++/v1/exception:82:
In file included from /usr/local/clang7/include/c++/v1/cstdlib:86:
/usr/local/clang7/include/c++/v1/stdlib.h:94:15: fatal error: ‘stdlib.h’ file not found
#include_next <stdlib.h>
^~~~~~~~~~
1 error generated.
make: *** [file95a6732c92.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file95a6732c92.cpp 2> file95a6732c92.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

I think the above is still a problem somehow. Perhaps @bgoodri can help out.

I am very thankful for your help and time. Now you have narrowed it down I can research this issue better, it was unclear the relevant part of the error. My last resort is to restore my computer to factory settings then start fresh.

Wait until @bgoodri replies.

That is almost never necessary. I would try doing

file.rename(from = "~/.R/Makevars", to = "~/.R/Makevars_backup")
1 Like

After I execute your code to rename the file, and ran my code again, the error message shows:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from :1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:82:
In file included from /usr/local/clang7/include/c++/v1/new:91:
In file included from /usr/local/clang7/include/c++/v1/exception:82:
In file included from /usr/local/clang7/include/c++/v1/cstdlib:86:
/usr/local/clang7/include/c++/v1/stdlib.h:94:15: fatal error: ‘stdlib.h’ file not found
#include_next <stdlib.h>
^~~~~~~~~~
1 error generated.
make: *** [file59a33a8d392.o] Error 1
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file59a33a8d392.cpp 2> file59a33a8d392.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

And what is

readLines("~/.Rprofile")

?

Running that code in Rstudio:

readLines(“~/.Rprofile”)
[1] “## WORKAROUND: https://github.com/rstudio/rstudio/issues/6692
[2] “## Revert to ‘sequential’ setup of PSOCK cluster in RStudio Console on macOS and R 4.0.0”
[3] “if (Sys.getenv("RSTUDIO") == "1" && !nzchar(Sys.getenv("RSTUDIO_TERM")) && "
[4] " Sys.info()["sysname"] == "Darwin" && getRversion() == "4.0.0") {”
[5] " parallel:::setDefaultClusterOptions(setup_strategy = "sequential")"
[6] “}”
Warning message:
In readLines(“~/.Rprofile”) : incomplete final line found on ‘~/.Rprofile’

And

readLines("~/.Renviron")

?

readLines(“~/.Renviron”)
[1] “# clang: start” “PATH="/usr/local/clang7/bin:${PATH}"”
[3] “# clang: end”

And

Sys.which("clang++")

?

Sys.which(“clang++”)
clang++
“/usr/local/clang7/bin/clang++”