Compilation error on macOS Mojave

Hi everyone, I’m new to RStan and failed at the first 8 schools example. I can’t find any solution online that works for me. Great thanks for any help!
I followed the installation instructions and everything seems good, but when I tried the 8 schools code, I got the following error message:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from file3b3c29d326c9.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:8:
In file included from /usr/local/clang4/bin/…/include/c++/v1/cstdlib:86:
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file3b3c29d326c9.cpp 2> file3b3c29d326c9.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

  • Operating System
    macOS Mojave 10.14.2
    RStudio 1.2.1244
  • RStan Version
    2.18.2
  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))

[1] “# The following statements are required to use the clang4 binary”
[2] “LDFLAGS= -L/usr/local/clang4/lib”
[3] “# End clang4 inclusion statements”
[4] “CC=/usr/local/clang4/bin/clang”
[5] “CXX=/usr/local/clang4/bin/clang++”
[6] “CXX1X=/usr/local/clang4/bin/clang++”
[7] “CXX98=/usr/local/clang4/bin/clang++”
[8] “CXX11=/usr/local/clang4/bin/clang++”
[9] “CXX14=/usr/local/clang4/bin/clang++”
[10] “CXX17=/usr/local/clang4/bin/clang++”

  • Output of devtools::session_info("rstan")

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.14.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
character(0)

other attached packages:
[1] rstan_2.18.2

loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 pillar_1.3.0 compiler_3.5.1 plyr_1.8.4
[5] bindr_0.1.1 prettyunits_1.0.2 methods_3.5.1 base64enc_0.1-3
[9] utils_3.5.1 tools_3.5.1 grDevices_3.5.1 pkgbuild_1.0.2
[13] tibble_1.4.2 gtable_0.2.0 pkgconfig_2.0.2 rlang_0.3.0.1
[17] cli_1.0.1 rstudioapi_0.9.0 parallel_3.5.1 loo_2.0.0
[21] bindrcpp_0.2.2 gridExtra_2.3 withr_2.1.2 dplyr_0.7.8
[25] graphics_3.5.1 datasets_3.5.1 stats_3.5.1 stats4_3.5.1
[29] grid_3.5.1 tidyselect_0.2.5 glue_1.3.0 inline_0.3.15
[33] base_3.5.1 R6_2.3.0 processx_3.2.0 ggplot2_3.1.0
[37] callr_3.0.0 purrr_0.2.5 magrittr_1.5 scales_1.0.0
[41] ps_1.2.1 StanHeaders_2.18.0-1 matrixStats_0.54.0 assertthat_0.2.0
[45] colorspace_1.3-2 lazyeval_0.2.1 munsell_0.5.0 crayon_1.3.4
If you are reporting a bug (thank you!) please use the issue tracker (github.com/stan-dev/rstan/issues) instead of the forums.

Thanks!