Setting up rstan for the first time

So, I am trying to set up the RStan interface for the first time.
I am trying to follow all the directions from this Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub.
I already installed the Rtools.
I run this:

install.packages("pkgbuild")

rt_path = gsub("\\","/",pkgbuild::rtools_path(),fixed=T)
rt_bin = paste0(substr(rt_path,1,nchar(rt_path)-4),"/mingw_$(WIN)/bin/")
writeLines(paste0('PATH="',rt_path,';${PATH}"'), con = "~/.Renviron")
writeLines(paste0('Sys.setenv(BINPREF = "',rt_bin,'")'), con = "~/.Rprofile")

then;

install.packages("jsonlite",type="source")

This is where the problem is. The output for the above codes is :

Installing package into ‘C:/Users/User/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/jsonlite_1.7.2.tar.gz'
Content type 'application/x-gzip' length 421716 bytes (411 KB)
downloaded 411 KB

* installing *source* package 'jsonlite' ...
** package 'jsonlite' successfully unpacked and MD5 sums checked
** using staged installation
** libs

*** arch - i386
C:/rtools40/ucrt64/mingw_32/bin/gcc  -I"C:/PROGRA~1/R/R-41~1.2/include" -DNDEBUG -Iyajl/api       -D__USE_MINGW_ANSI_STDIO   -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign  -c base64.c -o base64.o
sh: line 1: C:/rtools40/ucrt64/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-41~1.2/etc/i386/Makeconf:238: base64.o] Error 127
ERROR: compilation failed for package 'jsonlite'
* removing 'C:/Users/User/Documents/R/win-library/4.1/jsonlite'
* restoring previous 'C:/Users/User/Documents/R/win-library/4.1/jsonlite'
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status

Then, I followed the next step;

 Sys.getenv("BINPREF")
[1] "C:/rtools40/ucrt64/mingw_$(WIN)/bin/"

readLines("~/.Rprofile")
[1] "Sys.setenv(BINPREF = \"C:/rtools40/usr/mingw_$(WIN)/bin/\")"   
[2] "Sys.setenv(BINPREF = \"C:/rtools40/ucrt64/mingw_$(WIN)/bin/\")"

file.path(Sys.getenv("HOME"), ".Rprofile")
[1] "C:/Users/User/Documents/.Rprofile"

Restarting R session...

Sys.getenv("BINPREF")
[1] "C:/rtools40/ucrt64/mingw_$(WIN)/bin/"

As you can see it should produce this output

Sys.getenv("BINPREF")
[1] ""

but after the correction, I still got a different output and I still got errors when trying to install jsonlite package. I’ve been repeating this step over and over again and the same problem occurs. Please help me.

The issue is that you’re following the instructions for R3.x, you only need to follow the instructions for R4.x