For some very annoying reason, I will not be able to call my R-scripts directly at a data center. I am only allowed to call stata, and then stata will have to call my R scripts.
For now, I was trying to use the shell command:
capture cd C:\\Correct\Dir
shell "C:\\Program Files\R-3.1.2\bin\Rscript.exe" "myFile.R"
The paths are correct, but I only get a blue screen when running this in Stata, and nothing else happens. There is a message in the blue screen, but it disappears immediately, so I have no clue what it says.
How can I proceed debugging this? Is there a better way doing this? I'd prefer not using additional packages like rsource, as they need to be certified before installed in the data center and that is a lengthy process.
2 Answers 2
I think the presence of the double backslash is causing the problem. The following works for me:
stata
cd "path\of\choice"
shell "C:\Program Files\R\R-3.1.2\bin\Rscript.exe" "test.R"
test.r
setwd("path\\of\\choice")
data(mtcars)
mtcars
write.csv(mtcars, "cars.csv")
Comments
Here is one example (non-reproducible) of Stata calling R.
*----- CALL R -----
// location of input/output files for R
local dirq "`pdir'/proc_data/q_irepriv.csv" // input 1 -> arg1
local dirh "`pdir'/proc_data/h_nophincome.csv" // input 2 -> arg2
local dirRdta "`pdir'/proc_data/`dofile'.Rdta" // output -> arg3
local dirout "`pdir'/" // project_dir -> arg4
local dirout "`dofile'/" // do_file_stub -> arg5
// call -rsource- passing the locations as arguments
rsource using "`pdir'/r_files/`dofile'.R", ///
roptions(`" --vanilla --args "`dirq'" "`dirh'" "`dirRdta'" "`pdir'" "`dofile'" "')
*----- END OF R -----
I use rsource, a user-written command which you can download with ssc install rsource.
On another note, Stata favors the use of forward slashes in these cases. See Stata tip 65: Beware the backstabbing backslash, by Nick Cox.
Edit
You report a blue screen that disappears, with nothing else happening. This can be the result of R choking on some error within your R script. As an example:
The .r script contains:
# output OK
head(mtcars)
# provoke error
2+*2
and your Stata do-file contains:
shell "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" --no-save --no-restore --verbose "D:/Datos/rferrer/Desktop/rcars.r"
The above reproduces your report.
To debug, you can redirect output and error messages using OS shell commands. Instead of the latter, try:
shell "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" --no-save --no-restore --verbose "D:/Datos/rferrer/Desktop/rcars.r" > Routput.txt 2> Rerror.txt
This produces two files:
Routput.txt contains
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
and Rerror.txt contains
running
'C:\Program Files\R\R-3.0.3\bin\x64\Rterm.exe --slave --no-restore --no-save --no-restore --file=D:/Datos/rferrer/Desktop/rcars.r'
Error: inesperado '*' in "2+*"
Ejecución interrumpida
(The error is in Spanish, but that of course, is irrelevant.)
That is on Windows 7 Enterprise.
capture cd C:\\Correct\\Dir