Rstan error when running stan function

Hello,

I am very new to rstan. I have installed the package “rstan” in my Rstudio. However, whenever I run the function stan() (e.g., fit2=stan(file = here(“kids2.stan”), data = data, chains = 3, iter = 500)), I will be asked to install build tools, which says “Building R package from source requires installation of additional build tools; Do you want to install the additional tools now?” Then it will direct me to install XCode. However, I already have Xcode Command Line Tools installed and I don’t want to install the full Xcode package since it will take a lot of space. If I ignore the message which forces me to install XCode, I will not be able to run the stan() function. I will get an error message as follows:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error in compileCode(f, code, language = language, verbose = verbose) :
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrunError in if (nzchar(cxx)) { : argument is of length zeroCalls: → .shlib_internalIn addition: Warning message:In system(paste(MAKE, p1(paste(“-f”, shQuote(makefiles))), “compilers”), : running command ‘make -f ‘/Library/Frameworks/R.framework/Resources/etc/Makeconf’ -f ‘/Library/Frameworks/R.framework/Resources/share/make/shlib.mk’ -f ‘/Users/gavinleung/.R/Makevars’ compilers’ had status 1Execution halted
Error in sink(type = “output”) : invalid connection

I have googled and tried different ways to solve the issue but still could not figure out what the problem is here. Any help is very much appreciated!

Here are other specs that might be useful:

  • Operating System: macOS Ventura, Version 13.6.1
  • Rstudio Version: Version 2023.12.1+402
    Both R and Rstudio have been upgraded to the latest versions.

You might want to try cmdstanr, which is generally easier to install than RStan.

You need to install Xcode and then the Xcode command-line tools. The latter is just an interface to the former. Sorry it’s so big. If you want something more minimal you can brew install clang and use that. You want to verify it’s working before trying to install RStan. You should be able to open a terminal and type this:

clang++ --version

and get the version you’ve installed from either Xcode or brew.

Thank you for the reply! I just figured it out. I uninstalled the Xcode CLT, downloaded it directly from the Apply Developer Store website, and installed it again. This way the Rstan worked out! It seems installing Xcode CLT directly from the terminal did not work for my laptop. Anyways, it worked out, and thanks a lot!