问题描述
我正在尝试通过Windows命令提示符上的BAT文件运行R脚本。
I am trying to run R scripts via BAT files on Windows Command Prompt.
脚本需要一些R程序包,例如 data。表
, tidyR
等。
The scripts require a few R packages such as data.table
, tidyR
, etc.
出于操作原因,所有必需的R包和依赖项(包括 data.table
)安装在 C:\Users\username\Documents\R\R-3.5.1 \图书馆
。我不允许在这种环境中安装RStudio。
For operational reasons, all required R packages and dependencies (including data.table
) are installed at C:\Users\username\Documents\R\R-3.5.1\library
. I am not allowed to install RStudio in this environment.
当我尝试
C:\Program Files\R\ \R-3.5.1\bin\x64\Rscript.exe脚本。R
,我收到类似于
When I try "C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R
, I get an error similar to
如何通过命令提示符设置 .libPaths
指向软件包的正确位置(即 C:\ Users\username\Documents\R\R-3.5.1\library
)?
How can I set the .libPaths
via Command Prompt to point to the correct location of the packages (i.e. to C:\Users\username\Documents\R\R-3.5.1\library
)?
在此先感谢您。
推荐答案
免责声明:我不熟悉 R
。 / em>
来自:
默认情况下,未设置R_LIBS,而R_LIBS_USER设置为目录
'R / R主目录的.version $ platform-library / x.y(对于CRAN macOS构建为
'Library / R / xy / library'),对于R xyz
By default R_LIBS is unset, and R_LIBS_USER is set to directory ‘R/R.version$platform-library/x.y’ of the home directory (or ‘Library/R/x.y/library’ for CRAN macOS builds), for R x.y.z.
可以创建使用 set VARIABLE_NAME = YOUR_VALUE
批处理命令。
An environment variable can be created with set VARIABLE_NAME=YOUR_VALUE
batch command.
所以您的批处理文件应该是这样的:
So your batch file should probably be something like this:
cd /d "C:\INSERT_PATH_TO_DIRECTORY_CONTAINING_script.R"
set "R_LIBS=C:\Users\username\Documents\R\R-3.5.1\library"
"C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R
但是出于可移植性的原因(假设某位同事要求您提供脚本或计算机的副本) (建议去世)我建议将脚本,R库和批处理放入文件放在一个目录中,例如 C:\Users\username\Documents\R
。批处理文件 C:\Users\username\Documents\R\script.bat
变为:
However for portability reasons (let's say a collegue asks for a copy of your script or your computer dies) I suggest putting the script, R library and batch file in a single directory, let's say C:\Users\username\Documents\R
. The batch file C:\Users\username\Documents\R\script.bat
becomes:
cd /d "%~dp0"
set "R_LIBS=%~dp0R-3.5.1\library"
"%PROGRAMFILES%\R\R-3.5.1\bin\x64\Rscript.exe" "%~dpn0.R"
%PROGRAMFILES%
环境变量扩展到程序文件
文件夹的完整路径%〜dp0
扩展到目录的完整路径其中包含您的批处理文件,而%〜dpn0
是不带扩展名的批处理文件完整路径。
%PROGRAMFILES%
environment variable expands to full path of program files
folder, %~dp0
parameter expands to full path of a directory that holds your batch file, and %~dpn0
is a batch-file full path without extension.
注意%〜dp0R-3.5.1
不是错字,因为%〜dp0
包括尾部反斜杠。
Notice that %~dp0R-3.5.1
is not a typo because %~dp0
includes trailing backslash.
通过这种方式,您可以复制 C:\Users \username\Documents\R
到 D:\Users\SOMEOTHERNAME\Documents\R
,脚本仍将运行。
This way you can copy C:\Users\username\Documents\R
to D:\Users\SOMEOTHERNAME\Documents\R
and the script will still run.
如果您创建脚本的另一个版本,只需复制批处理文件,使其与脚本具有相同的文件名,但 .bat
扩展名而不是 .R
,它应该调用新脚本-已证明在调试和分发脚本时非常方便。
If you create another version of your script, just copy the batch file so that it has same filename as your script but .bat
extension instead of .R
and it should call the new script - this has proven to be very handy when debugging and distributing scripts.
或者,如果您想单独安装库,则可能要使用%HOMEDRIVE %% HOMEPATH%
,它会扩展为 C:\Users\用户名
。
Alternatively, if you would rather install libraries separately you may want to use %HOMEDRIVE%%HOMEPATH%
which expands to C:\Users\username
.
提取正确的 Documents
文件夹路径以及 R
安装路径是可能的,但是需要阅读注册表,因此更加复杂。
Extracting proper Documents
folder path, as well as R
installation path is possible but requires reading the registry and thus is a bit more complicated.
这篇关于设置.libPaths()以使用Rscript.exe从命令行运行R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!