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 folderin 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.
I think something like the following will do what you want:
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.
Thanks to @Extrarius, I corrected the code, such that it should run first time. I was rejected to do this as an edit.