Can't load package in R batch mode when called from Stata

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

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  

回答1:

For some reason calling R from Stata via winexec (or shell or !) opens a different command prompt than does opening the command prompt from the start window. At least in my install, this loads a different set of environment variables so that the library path is the admin library path.

> .libPaths() [1] "C:/Program Files/R/R-3.1.2/library" 

However, I use a non-admin library, so that my library path includes both the admin and user libraries.

> .libPaths() [1] "C:/Users/richa_000/Documents/R/win-library/3.1" [2] "C:/Program Files/R/R-3.1.2/library"             

I tried the Stata package rsource from ssc (comments above), but this didn't solve the environment problem. My hack is just to append to the library path in the R script as follows.

.libPaths(c(.libPaths(), "C:/Users/richa_000/Documents/R/win-library/3.1")) 

This solves the problem and yields the correct library path (without creating a second library).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!