nov
nov

Reputation: 167

call R from Stata with rsource - not working

I want to create some random variables in R and return them in Stata, all written in one do file. I used rsource with option terminator(). This is a short version of my do file (just to mention, R commands are taken from R file that is working, and by calling these diretly in R it does produce X.dta)...

clear
set more off

cd "C:\Users\....\Desktop\R_stata"


rsource, terminator(END_OF_R)  rpath(C:\Program Files\R\R-3.1.1\bin\R.exe) 


library(mvtnorm);
library(foreign);


xmean<-rep(0,100);
x1Sigma<- diag(100);
x2Sigma<- 2 * diag(100);

X1<-rmvnorm(n=1, mean=xmean, sigma=x1Sigma);
X1<- t(X1);
X2<-rmvnorm(n=1, mean=xmean, sigma=x2Sigma);
X2<- t(X2);

write.dta(data.frame(X1, X2), "C:/Users/...../Desktop/R_stata/X.dta");


END_OF_R

use X.dta, replace

Upvotes: 3

Views: 702

Answers (1)

dimitriy
dimitriy

Reputation: 9460

I think you need to add roptions("--vanilla") to your rsource. Without that option, I get

file X.dta not found
r(601);

With it, it works perfectly for me.

Upvotes: 4

Related Questions