I would like to run from Stata a simple R script that reads SAS a data file (using the sas7bdat package) and writes a Stata data file (using the foreign package). I can call the R script with CMD BATCH and it runs, but it is unable to use the sas7bdat package.
Here is the Stata script.
clear winexec "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R Here is the R script in temp.R.
# install.packages("sas7bdat") # install.packages("foreign") library("sas7bdat") library("foreign") # # test file (my file is local, so this line is commented out) # download.file(url="http://www.ats.ucla.edu/stat/sas/dae/logit.sas7bdat", # destfile="temp.sas7bdat", # mode="wb") temp <- read.sas7bdat("temp.sas7bdat") write.dta(temp, "temp.dta") If I run this script from the R gui, then everything works fine. Likewise I run it from the Windows command prompt with "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R. But when I run it from Stata with winexec (or shell) it fails.
Here is the contents of temp.Rout when I run the R script from Stata.
R version 3.1.0 (2014-04-10) -- "Spring Dance" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > # install.packages("sas7bdat") > # install.packages("foreign") > library("sas7bdat") Error in library("sas7bdat") : there is no package called 'sas7bdat' Execution halted FWIW, I am using Stata 11.2 on Windows 8.1 update 1.
Update:
When I run temp.R from the Windows command prompt I get the following.
C:\Users\richa_000\Desktop\SOquestion>"C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
yields a temp.Rout with
R version 3.1.0 (2014-04-10) -- "Spring Dance" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > # install.packages("sas7bdat") > # install.packages("foreign") > library("sas7bdat") > library("foreign") > > # # test file (my file is local, so this line is commented out) > # download.file(url="http://www.ats.ucla.edu/stat/sas/dae/logit.sas7bdat", > # destfile="temp.sas7bdat", > # mode="wb") > > temp <- read.sas7bdat("temp.sas7bdat") > write.dta(temp, "temp.dta") > > proc.time() user system elapsed 0.32 0.03 0.34