这是 Stata 调用 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 -----
我使用rsource,这是一个用户编写的命令,您可以使用ssc install rsource 下载它。
另一方面,Stata 倾向于在这些情况下使用正斜杠。请参阅 Nick Cox 的 Stata tip 65: Beware the backstabbing backslash。
编辑
您报告蓝屏消失,没有其他任何事情发生。这可能是 R 因 R 脚本中的某些错误而窒息的结果。举个例子:
.r 脚本包含:
# output OK
head(mtcars)
# provoke error
2+*2
并且您的 Stata do 文件包含:
shell "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" --no-save --no-restore --verbose "D:/Datos/rferrer/Desktop/rcars.r"
以上内容转载了您的报告。
要进行调试,您可以使用 OS shell 命令重定向输出和错误消息。而不是后者,尝试:
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
这会产生两个文件:
Routput.txt 包含
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
并且Rerror.txt包含
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
(错误是西班牙语的,但那当然是无关紧要的。)
这是在 Windows 7 企业版上。