add latest R installation path from registry to PA

2020-07-20 03:53发布

Hi I'm new to windows batch.

I want to hand out a runMe.bat file to co-workers calling Rscript myRfile.R to process some data files. 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.

I would like the .bat file to lookup path of latest installed R and add [that directory]\bin\i386\ to PATH temporarily.

I imagine to:

  • iterate the subfolders of registry HKEY_LOCAL_MACHINE\Software\Rcore\R\ to find the last and latest R-version folder

  • in this registry subdirectory get the **installPath** e.g. 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. My co-workers probably have quite restricted windows administrator privileges anyway.

Thank you very much!

Bonus: My company mainly has 32bit Windows OS installations, but will upgrade sometime in a distant future. I don't mind only executing R i386 version. Runtime and memory req. is very modest.

2条回答
够拽才男人
2楼-- · 2020-07-20 04:34

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). 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. The inner if statement is just to make sure blank lines are ignored.

Next is a basic error check to ensure that rkey was set (in case the registry key doesn't exist, for ex).

The next for loop should only iterate once, and extracts just the value part from the installPath value in the selected key. The for is just used to skip irrelevant lines and tokens. 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.

查看更多
Summer. ? 凉城
3楼-- · 2020-07-20 04:58

Thanks to @Extrarius, I corrected the code, such that it should run first time. 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
查看更多
登录 后发表回答