从 Stata 调用时无法以 R 批处理模式加载包
Can't load package in R batch mode when called from Stata
我想从 Stata 运行 一个简单的 R 脚本,它读取 SAS 数据文件(使用 sas7bdat
包)并写入 Stata 数据文件(使用foreign
包)。我可以使用 CMD BATCH
调用 R 脚本并运行,但它无法使用 sas7bdat
包。
这是 Stata 脚本。
clear
winexec "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
这是temp.R
中的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")
如果我从 R gui 运行这个脚本,那么一切正常。同样,我使用 "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
从 Windows 命令提示符运行它。但是当我用winexec
(或shell
)从Stata运行它时它失败了。
这是我从 Stata 运行 R 脚本时 temp.Rout
的内容。
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,我在 Windows 8.1 更新 1.
上使用 Stata 11.2
更新:
当我从 Windows 命令提示符运行 temp.R 时,我得到以下信息。
C:\Users\richa_000\Desktop\SOquestion>"C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
产生 temp.Rout
和
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
出于某种原因,通过 winexec
(或 shell
或 !
)从 Stata 调用 R
会打开一个不同于从一开始就打开命令提示符的命令提示符window。至少在我的安装中,这加载了一组不同的环境变量,因此库路径是管理库路径。
> .libPaths()
[1] "C:/Program Files/R/R-3.1.2/library"
但是,我使用的是非管理库,因此我的库路径包括管理库和用户库。
> .libPaths()
[1] "C:/Users/richa_000/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.2/library"
我尝试了 ssc
的 Stata 包 rsource
(上面的评论),但这并没有解决环境问题。我的 hack 只是附加到 R 脚本中的库路径,如下所示。
.libPaths(c(.libPaths(), "C:/Users/richa_000/Documents/R/win-library/3.1"))
这解决了问题并生成了正确的库路径(无需创建第二个库)。
我想从 Stata 运行 一个简单的 R 脚本,它读取 SAS 数据文件(使用 sas7bdat
包)并写入 Stata 数据文件(使用foreign
包)。我可以使用 CMD BATCH
调用 R 脚本并运行,但它无法使用 sas7bdat
包。
这是 Stata 脚本。
clear
winexec "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
这是temp.R
中的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")
如果我从 R gui 运行这个脚本,那么一切正常。同样,我使用 "C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
从 Windows 命令提示符运行它。但是当我用winexec
(或shell
)从Stata运行它时它失败了。
这是我从 Stata 运行 R 脚本时 temp.Rout
的内容。
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,我在 Windows 8.1 更新 1.
上使用 Stata 11.2更新:
当我从 Windows 命令提示符运行 temp.R 时,我得到以下信息。
C:\Users\richa_000\Desktop\SOquestion>"C:\Program Files\R\R-3.1.0\bin\x64\R.exe" CMD BATCH temp.R
产生 temp.Rout
和
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
出于某种原因,通过 winexec
(或 shell
或 !
)从 Stata 调用 R
会打开一个不同于从一开始就打开命令提示符的命令提示符window。至少在我的安装中,这加载了一组不同的环境变量,因此库路径是管理库路径。
> .libPaths()
[1] "C:/Program Files/R/R-3.1.2/library"
但是,我使用的是非管理库,因此我的库路径包括管理库和用户库。
> .libPaths()
[1] "C:/Users/richa_000/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.2/library"
我尝试了 ssc
的 Stata 包 rsource
(上面的评论),但这并没有解决环境问题。我的 hack 只是附加到 R 脚本中的库路径,如下所示。
.libPaths(c(.libPaths(), "C:/Users/richa_000/Documents/R/win-library/3.1"))
这解决了问题并生成了正确的库路径(无需创建第二个库)。