1

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.

asked Apr 14, 2015 at 15:36
2
  • Just noticed that you have double backslashes after drive letter, but not for other folders. Does it make a difference if you do capture cd C:\\Correct\\Dir Commented Apr 14, 2015 at 15:45
  • @user2633645 I'll check that next time I'm in the data center. Commented Apr 14, 2015 at 15:47

2 Answers 2

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")
answered Apr 14, 2015 at 15:56
Sign up to request clarification or add additional context in comments.

Comments

2

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.

answered Apr 14, 2015 at 16:19

3 Comments

Regarding your note, I quote from that article: "In fact, you can mix forward and backward slashes willy-nilly in directory or filenames within Stata for Windows, so long as you do not use a backslash just before a left quotation mark."
True. The wording may not covey the original intention but it was meant primarily as a recommendation. I never claimed it was the source of your problem. I too quote from that article: "The tidiest solution is to use forward slashes consistently ..." It's less confusing and makes your code portable across operating systems (potentially, at least). You may consider it a matter of taste and you can act upon that, but I still stand by the recommendation.
Wish I could turn my errors to Spanish too - they sound much cooler! ;)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.