1

I am working on a project where I need to run an R script within Stata. I have the following code written:

shell "/Users/alexanderrbilly/Desktop/Neel/cleaner.R" CMD BATCH "cleaner.R"

Despite even changing the command directory before this (which shouldn't matter), I keep getting the same error:

/bin/bash: /Users/alexanderrbilly/Desktop/Neel/cleaner.R: Permission denied

Let me know if I'm doing something erroneous or how I may get around this issue.

JuliusBilly
  • 162
  • 7
  • You are not calling an R executable only the script which is only a text file. Consider the automated Rscript: `/path/to/Rscript /path/to/cleaner.R` – Parfait Aug 04 '17 at 18:20

1 Answers1

1

Your command line is not correct.

Your string "/Users/alexanderrbilly/Desktop/Neel/cleaner.R" should be a path to your R executable.

If you are on Windows. Say a file K:\tmp\random.R contains the code write.csv(rnorm(10), "a.csv"). Then, you can write in Stata:

cd K:\tmp
shell C:\APPLI\R\R-3.4.1\bin\x64\R.exe CMD BATCH random.R

Of course, change the path to your R executable to fit your installation.

If you are on Linux, the error probably comes from the fact that cleaner.R does not have the execute permission. You may also have a look at What's the best way to use R scripts on the command line (terminal)?

eoraptor
  • 56
  • 3
  • Cool! I used `file.path(R.home("bin"), "R")` to identify the R executable path and it seems to be working right now (for those unsure of how to extract the path). I used information from this page: [Reference](https://stackoverflow.com/questions/33798115/command-to-see-r-path-that-rstudio-is-using) – JuliusBilly Aug 04 '17 at 19:13
  • So you are on macOS. The problem is not much different, but you have to find the path, if R is not already in the PATH environment variable: in a Terminal, does the command "R" launch something? You may also start the R application and type `Sys.getenv("PATH")` to find out where the executable may be. – eoraptor Aug 04 '17 at 19:16
  • Oh, I didn't see you changed your comment. It's another way to find. – eoraptor Aug 04 '17 at 19:18
  • @JuliusBilly You may also be interested by the package rsource from ssc. For more information, type `ssc describe rsource` in Stata. To install, type `ssc install rsource`. – eoraptor Aug 05 '17 at 09:55