简体   繁体   English

设置.libPaths()以使用Rscript.exe从命令行运行R脚本

[英]Setting .libPaths() For Running R Scripts From Command Line Using Rscript.exe

I am trying to run R scripts via BAT files on Windows Command Prompt. 我试图通过Windows命令提示符上的BAT文件运行R脚本。

The scripts require a few R packages such as data.table , tidyR , etc. 脚本需要一些R包,例如data.tabletidyR等。

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 . 出于操作原因,所有必需的R软件包和依赖项(包括data.table )都安装在C:\\Users\\username\\Documents\\R\\R-3.5.1\\library I am not allowed to install RStudio in this environment. 我不允许在此环境中安装RStudio。

When I try "C:\\Program Files\\R\\R-3.5.1\\bin\\x64\\Rscript.exe" script.R , I get an error similar to 当我尝试"C:\\Program Files\\R\\R-3.5.1\\bin\\x64\\Rscript.exe" script.R ,出现类似的错误

Error in library(data.table) : there is no package called 'data.table' Execution halted 库(data.table)中的错误:没有名为“ data.table”的程序包中止执行

How can I set the .libPaths via Command Prompt to point to the correct location of the packages (ie to C:\\Users\\username\\Documents\\R\\R-3.5.1\\library )? 如何通过命令提示符设置.libPaths指向软件包的正确位置(即C:\\Users\\username\\Documents\\R\\R-3.5.1\\library )?

Thank you in advance. 先感谢您。

Disclaimer: I'm unfamiliar with R . 免责声明:我不熟悉R

From R: Search paths : R:搜索路径

The library search path is initialized at startup from the environment variable R_LIBS (which should be a colon-separated list of directories at which R library trees are rooted) followed by those in environment variable R_LIBS_USER. 库搜索路径在启动时从环境变量R_LIBS(应该是R库树所在的目录的冒号分隔的目录列表)中初始化,然后在环境变量R_LIBS_USER中进行初始化。 Only directories which exist at the time will be included. 仅包含当时存在的目录。

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/xy/library' for CRAN macOS builds), for R xyz 默认情况下,未设置R_LIBS,并且R_LIBS_USER设置为R xyz的主目录的目录'R / R.version $ platform-library / x.y(对于CRAN macOS构建为'Library / R / xy / library')

An environment variable can be created with set VARIABLE_NAME=YOUR_VALUE batch command. 可以使用set VARIABLE_NAME=YOUR_VALUE批处理命令来创建环境变量

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

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 . 但是出于可移植性的原因(假设某位同事要求您提供脚本副本或计算机死机),我建议将脚本,R库和批处理文件放在一个目录中,例如C:\\Users\\username\\Documents\\R The batch file C:\\Users\\username\\Documents\\R\\script.bat becomes: 批处理文件C:\\Users\\username\\Documents\\R\\script.bat变为:

cd /d "%~dp0"
set "R_LIBS=%~dp0R-3.5.1\library"
"%PROGRAMFILES%\R\R-3.5.1\bin\x64\Rscript.exe" "%~dpn0.R"

%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. %PROGRAMFILES%环境变量扩展到program files夹的完整路径, %~dp0 参数扩展到保存批处理文件的目录的完整路径, %~dpn0是不带扩展名的批处理文件完整路径。

Notice that %~dp0R-3.5.1 is not a typo because %~dp0 includes trailing backslash. 注意, %~dp0R-3.5.1不是一个错字,因为%~dp0包含尾部反斜杠。

This way you can copy C:\\Users\\username\\Documents\\R to D:\\Users\\SOMEOTHERNAME\\Documents\\R and the script will still run. 这样,您可以将C:\\Users\\username\\Documents\\R复制到D:\\Users\\SOMEOTHERNAME\\Documents\\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. 如果您创建脚本的另一个版本,只需复制批处理文件,使其具有与脚本相同的文件名,但扩展名为.bat而不是.R ,并且应调用新脚本-在调试和分发时,这非常方便脚本。

Alternatively, if you would rather install libraries separately you may want to use %HOMEDRIVE%%HOMEPATH% which expands to C:\\Users\\username . 另外,如果您想单独安装库,则可能要使用%HOMEDRIVE%%HOMEPATH% ,它会扩展为C:\\Users\\username

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. 可以提取正确的Documents文件夹路径以及R安装路径,但需要读取注册表,因此更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM