I was using [scoop](https://scoop.sh/) to install R (via the main bucket), RStudio and Quarto (both via [cderv/r-bucket](https://github.com/cderv/r-bucket)). However, I found a wired issue that RStudio errored when rendering quarto documents saying that:

```
ERROR: Error executing 'C:\Users\hongy\scoop\apps\r\current\bin\Rscript': 系统找不到指定的文件。 (os error 2)

Unable to locate an installed version of R.
Install R from https://cloud.r-project.org/
```

But directly running `quarto render test.qmd` from command line works.

After some search though the codebase, I found that `rBinaryPath("Rscript")` was called to run `Rscript` with given arguments. `rBinaryPath()` internally uses `QUARTO_R`, `R_HOME` and Windows Registry entry to locate the `bin` directory under the R installation folder. Then use `bin/Rscript.exe` as the target path. 

https://github.com/quarto-dev/quarto-cli/blob/af5c051e8251d3a0535f2e8dcf62d011259c8fb2/src/core/run/r.ts#L14-L33

However, this did not work if R was installed via scoop in RStudio. There are several related issues here:

If R was installed via scoop:

1. There is no Windows Register key written
2. The `Rscipt.exe` was only at `bin\x64|x32\Rscript.exe`, and scoop did not create links for `Rscript.exe` under the `bin` folder. See: https://github.com/ScoopInstaller/Main/blob/master/bucket/r.json#L22-L40
3. Shims for `Rscript.exe`, `R.exe`, `Rterm.exe` and etc. were created under `~/scoop/shims`. This folder was added to the user PATH so that every other program can access them. This is why directly running `quarto render` from the command line works. `rBinaryPath("Rscript")` will use the path that `which("Rscript")` returns.

However, when quarto was called in R, since the `R_HOME` environment variable was automatically created. `rBinaryPath("Rscript")` will use `R_HOME/bin/Rscript.exe` which does not exist.

One solution maybe is instead of directly returning `R_HOME/bin/Rscript.exe`:
1. Check if the path exists
2. If not, try testing `R_HOME/bin/{arch}/Rscript.exe`
cderv
2022-05-27T06:23:03Z
hongyuanjia
closed
Failed to locate `Rscript.exe` if R was installed via scoop in RStudio
https://github.com/quarto-dev/quarto-cli/issues/995