2

I want to run an R script through stata. I can successfully open the script this way:

shell open -a RStudio "PATHWAY/filename.R"

And in terminal, I can successfully run the script through the command:

Rscript "PATHWAY/filename.R"

I thought that shell allowed me to pass Stata terminal commands. However, shell Rscript "PATHWAY/filename.R" returns the error /bin/bash: Rscript: command not found.

I see that others execute R scripts via something like

shell "C:\Program Files\R\R-2.15.1\bin\x64\R.exe" CMD BATCH test.R

However, I am unsure about the equivalent R command pathway on my Mac. (Or why I need a pathway at all, since I can execute the file in terminal w/ the mere command Rscript, and can open the file w/ shell in stata, without a pathway.) Following notes here, I find that file.path(R.home("bin"), "R") returns "/Library/Frameworks/R.framework/Resources/bin/R", but adding this path way to the command (shell /Library/Frameworks/R.framework/Resources/bin/R/R.exe "PATHWAY/filename.R") doesn't help at all.

Additionally, I am aware of the Rsource package, but I think this is more capability than I need. I don't want to run the R code within Stata, or import R objects into Stata after running. I merely want Stata to tell terminal "Run this particular R script," and then wait for the run to complete before continuing on with the Stata code. So I think shell is the right avenue, if I can get it to work.

Would appreciate mac-specific advice! Cross-posted on Statalist.

Leah

Leah Bevis
  • 317
  • 1
  • 11
  • 1
    It looks like the path `/Library/Frameworks/R.framework/Resources/bin/R/R.exe` is calling the R executable, not Rscript. You can likely execute your R code from within Stata using `shell "/Library/Frameworks/R.framework/Resources/bin/R/R" CMD BATCH "PATHWAY/filename.R"`. Alternatively, you can search the path of the R executable given by RStudio to find the Rscript executable and try calling that directly in place of `Rscript "PATHWAY/filename.R"`. Also, consider looking at environment variables to see that `$PATH` (or OS X equivalent) is pointing to the Rscript executable. – ander2ed Oct 25 '17 at 21:44
  • Thank you! That ALMOST works --- if I get rid of the last slash R, it does work. So this runs: `shell "/Library/Frameworks/R.framework/Resources/bin/R" CMD BATCH "PATHWAY/filename.R"`. Thanks a lot! Additionally, this vanilla command runs: `shell /Library/Frameworks/R.framework/Resources/bin/R --vanilla – Leah Bevis Oct 26 '17 at 11:53
  • Oops, yeah forgot to remove that extra `/R`. But honestly I can't speak to the difference between the two methods - this is largely new territory for me. I've used `Rscript` and `R CMD BATCH` under Windows and Linux, but have never had issues and never tried via `Shell` in Stata. You might find Appendix B from the R-intro docs to be worth reading: https://cran.r-project.org/doc/manuals/R-intro.pdf – ander2ed Oct 26 '17 at 13:28
  • Wow, thank you! Yes, Appendix B is very helpful, indeed. Thanks so much for pointing it out! – Leah Bevis Oct 29 '17 at 21:24

0 Answers0