Calling R from Stata with rsource - not working
I want to create some random variables in R and put them back in Stata, all written to one do file. I used rsource
with option terminator()
. This is a short version of my do file (just for the sake of mentioning, the R commands are taken from the R file that works, and by calling it in R it produces 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
+3
source to share