Error running rstan example model

I am new to rstan and have just installed it to the latest version of R and Rstudio on Windows 10.
To verify the installation I ran the example model

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

giving the follwing error message:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file5140109d7ca8.o:file5140109d7ca8.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file5140109d7ca8.o:file5140109d7ca8.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file5140109d7ca8.o:file5140109d7ca8.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file5140109d7ca8.o:file5140109d7ca8.cpp:(.text$_ZN3tbb10interface

Can anyone please tell me what it means and how to solve it?

See the Getting Started guide

Thanks, I tried to restart R and reinstall rstan and StanHeaders from source according to the installation guide but it produced the following message:

  • installing source package ‘StanHeaders’ …
    ** package ‘StanHeaders’ successfully unpacked and MD5 sums checked
    ERROR: cannot remove earlier installation, is it in use?
  • removing ‘C:/Users/AndreasNi/AppData/Local/R/win-library/4.2/StanHeaders’
  • restoring previous ‘C:/Users/AndreasNi/AppData/Local/R/win-library/4.2/StanHeaders’
    Warning in file.copy(lp, dirname(pkgdir), recursive = TRUE, copy.date = TRUE) :
    problem copying C:\Users\AndreasNi\AppData\Local\R\win-library\4.2\00LOCK-StanHeaders\StanHeaders\libs\x64\StanHeaders.dll to C:\Users\AndreasNi\AppData\Local\R\win-library\4.2\StanHeaders\libs\x64\StanHeaders.dll: Permission denied
    Warning in install.packages :
    installation of package ‘StanHeaders’ had non-zero exit status
  • installing source package ‘rstan’ …
    ** package ‘rstan’ successfully unpacked and MD5 sums checked
    ERROR: cannot remove earlier installation, is it in use?
  • removing ‘C:/Users/AndreasNi/AppData/Local/R/win-library/4.2/rstan’
  • restoring previous ‘C:/Users/AndreasNi/AppData/Local/R/win-library/4.2/rstan’
    Warning in file.copy(lp, dirname(pkgdir), recursive = TRUE, copy.date = TRUE) :
    problem copying C:\Users\AndreasNi\AppData\Local\R\win-library\4.2\00LOCK-rstan\rstan\libs\x64\rstan.dll to C:\Users\AndreasNi\AppData\Local\R\win-library\4.2\rstan\libs\x64\rstan.dll: Permission denied
    Warning in install.packages :
    installation of package ‘rstan’ had non-zero exit status

The section I linked did not say to install from source, it said to install the preview version of rstan.

First we’ll try making sure that your previous installations are removed.

Restart R, making sure that no packages or objects get loaded, then run:

remove.packages(c("StanHeaders", "rstan"))

Then run the code to install the preview versions that I linked to earlier:

install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Done. Both StanHeaders and rstan are reinstalled from the links.

package ‘StanHeaders’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\AndreasNi\AppData\Local\Temp\RtmpsD13RQ\downloaded_packages

package ‘rstan’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\AndreasNi\AppData\Local\Temp\RtmpsD13RQ\downloaded_packages

Great! And does the example model run now?

Yes it does :)
Thanks Andrew!