简体   繁体   English

将最新的R安装路径从注册表添加到PATH windows 7/8/10

[英]add latest R installation path from registry to PATH windows 7/8/10

Hi I'm new to windows batch. 嗨,我是Windows批处理的新手。

I want to hand out a runMe.bat file to co-workers calling Rscript myRfile.R to process some data files. 我想将一个runMe.bat文件分发给同事调用Rscript myRfile.R来处理一些数据文件。 But my co-workers have notoriously installed R various places and I cannot expect them to know how to add Rscript to PATH or even to code in R. 但我的同事众所周知地安装了R各种各​​样的地方,我不能指望他们知道如何将Rscript添加到PATH甚至是在R中编码。

I would like the .bat file to lookup path of latest installed R and add [that directory]\\bin\\i386\\ to PATH temporarily. 我想.bat文件查找最新安装的R的路径,并暂时将[该目录] \\ bin \\ i386 \\添加到PATH。

I imagine to: 我想:

  • iterate the subfolders of registry HKEY_LOCAL_MACHINE\\Software\\Rcore\\R\\ to find the last and latest R-version folder 迭代注册表HKEY_LOCAL_MACHINE\\Software\\Rcore\\R\\ 的子文件夹以查找最新和最新的R版本文件夹

  • in this registry subdirectory get the **installPath** eg keyValue = "c:\\R\\R-3.2.2\\"

  • concatenate with "\\bin\\i386\\" -> c:\\R\\R-3.2.2\\bin\\i386\\ ->Rpath

  • PATH%PATH%;Rpath

  • Rscript myRfile.R

I prefer that the Rpath is not permanently added to PATH. 我更喜欢Rpath没有永久添加到PATH。 My co-workers probably have quite restricted windows administrator privileges anyway. 无论如何,我的同事可能已经完全限制了Windows管理员权限。

Thank you very much! 非常感谢你!

Bonus: My company mainly has 32bit Windows OS installations, but will upgrade sometime in a distant future. 奖金:我的公司主要有32位Windows操作系统安装,但会在不久的将来升级。 I don't mind only executing R i386 version. 我不介意只执行R i386版本。 Runtime and memory req. 运行时和内存需求。 is very modest. 很谦虚。

I think something like the following will do what you want: 我觉得像下面这样的东西会做你想要的:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET RKEY=
SET RPATH=
FOR /F "tokens=* skip=2" %%L IN ('reg.exe QUERY HKLM\Software\R-core\R /f * /k ^| sort') DO (
    IF NOT "%%~L"=="" SET "RKEY=%%~L"
)
IF NOT DEFINED RKEY (
    ECHO Unable to qyery registry key HKLM\Software\Rcore\R
    EXIT /B 1
)
FOR /F "tokens=2* skip=2" %%A IN ('REG QUERY %RKEY% /v "installPath"') DO (
    IF NOT "%%~B"=="" SET "RPATH=%%~B"
)
IF NOT DEFINED RPATH (
    ECHO Unable to query registry value %RKEY%\installPath
    EXIT /B 2
)
IF NOT EXIST "%RPATH%" (
    ECHO Found path for R (%RPATH%^) does not exist
    EXIT /B 3
)
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
    SET "PATH=%RPATH%\bin\x64;%PATH%"
) ELSE (
    SET "PATH=%RPATH%\bin\i386;%PATH%"
)
Rscript myscript.r

First, we enable 'local' mode so all variables we set will revert when the batch file exits (even if you use 'CALL' to invoke it). 首先,我们启用“本地”模式,以便我们设置的所有变量将在批处理文件退出时恢复(即使您使用“CALL”调用它)。 Next, we unset the two variables used, so we can test whether they are set by later code. 接下来,我们取消设置使用的两个变量,因此我们可以测试它们是否由以后的代码设置。

The first for loop will execute once per result, so RKEY ends up set to the last key under \\R, and sort will hopefully order them such that the newest installation will end up last. 第一个for循环将每个结果执行一次,因此RKEY最终设置为\\ R下的最后一个键,并且排序将有希望对它们进行排序,以使最新的安装最终结束。 The inner if statement is just to make sure blank lines are ignored. 内部if语句只是为了确保忽略空行。

Next is a basic error check to ensure that rkey was set (in case the registry key doesn't exist, for ex). 接下来是一个基本的错误检查,以确保设置了rkey(如果注册表项不存在,则为ex)。

The next for loop should only iterate once, and extracts just the value part from the installPath value in the selected key. 下一个for循环应该只迭代一次,并从选定键中的installPath值中仅提取值部分。 The for is just used to skip irrelevant lines and tokens. for仅用于跳过不相关的行和标记。 Then a test whether the value was found, and whether the found value actually exists or not. 然后测试是否找到了值,以及找到的值是否确实存在。

Finally, update the path based on the architecture, and run the script. 最后,根据体系结构更新路径,然后运行脚本。

Thanks to @Extrarius, I corrected the code, such that it should run first time. 感谢@Extrarius, 我更正了代码,以便它应该第一次运行。 I was rejected to do this as an edit. 作为编辑,我被拒绝这样做。

@ECHO OFF
ECHO Searching for install path of latest version of R in registry...
SETLOCAL ENABLEEXTENSIONS REM This line will reset path when return
SET RKEY=
SET RPATH=
FOR /F "tokens=* skip=2" %%L IN ('reg.exe QUERY HKLM\Software\R-core\R /f * /k ^| sort') DO (
    IF NOT "%%~L"=="" SET "RKEY=%%~L"
)
IF NOT DEFINED RKEY (
    ECHO Unable to query registry key HKLM\Software\R-core\R
    EXIT /B 1
)
FOR /F "tokens=2* skip=2" %%A IN ('REG QUERY %RKEY% /v "installPath"') DO (
    IF NOT "%%~B"=="" SET "RPATH=%%~B"
)
IF NOT DEFINED RPATH (
    ECHO Unable to query registry value %RKEY%\installPath
    EXIT /B 2
)
IF NOT EXIST "%RPATH%" (
    ECHO Found path for R (%RPATH%^) does not exist
    EXIT /B 3
)
SET OLDPATH=%PATH%
IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
    SET PATH=%RPATH%\bin\x64;%OLDPATH%
    ECHO Found %RPATH%\bin\x64
) ELSE (
    SET PATH=%RPATH%\bin\i386;%OLDPATH%
    ECHO Found %RPATH%\bin\i386
)

Rscript myscript.R

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

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