Opened 14 years ago

Closed 12 years ago

#1149 closed enhancement (fixed)

WinGrass - load R-installation-path dynamically into PATH

Reported by: hellik Owned by: grass-dev@…
Priority: normal Milestone: 7.0.0
Component: Startup Version: svn-trunk
Keywords: wingrass, path, R Cc:
CPU: x86-32 Platform: MSWindows Vista

Description

Hi,

at the moment the WinGrass-Installer reads the Windows-registry during installation for the R-installation-path

http://trac.osgeo.org/grass/browser/grass/trunk/mswindows/GRASS-Installer.nsi#L219

ReadRegStr $R_HKLM_INSTALL_PATH HKLM "Software\R-core\R" "InstallPath"
ReadRegStr $R_HKCU_INSTALL_PATH HKCU "Software\R-core\R" "InstallPath"

and writes this, if found, to the grassXX.bat

http://trac.osgeo.org/grass/browser/grass/trunk/mswindows/GRASS-Installer.nsi#L728

${If} $R_HKLM_INSTALL_PATH != ""
FileWrite $0 'set PATH=$R_HKLM_INSTALL_PATH\bin;%PATH%$\r$\n'
${EndIf}
${If} $R_HKCU_INSTALL_PATH != ""
FileWrite $0 'set PATH=$R_HKCU_INSTALL_PATH\bin;%PATH%$\r$\n'
${EndIf}

this is a very statically approach to get the R-installation-path into the %PATH% and R working in the Grass-delivered command-line. that means, if R is upadated/upgraded, the Grass-delivered command-line can't find R in the %PATH% anymore.

a dynamically approach to add the R-installation-path to %PATH% would be better.

from the R-Windows-FAQ: http://cran.r-project.org/bin/windows/base/rw-FAQ.html#Does-R-use-the-Registry_003f

2.17 Does R use the Registry?

Not when R itself is running.

When you run the R installer, there are options (under `Select Additional Tasks') to `Save version number in registry' and (for Administrator installs) `Associate R with .RData files'.

If you tick the first option, the following string entries are added to the Windows registry:

    * HKEY_LOCAL_MACHINE\Software\R-core\R\Current Version contains the version number, currently 2.11.1.
    * HKEY_LOCAL_MACHINE\Software\R-core\R\[version]\InstallPath (where [version] is currently 2.11.1) contains the path to the R home directory. 

If you do not have administrative privileges on the machine while running the installer, then the entries are created under HKEY_CURRENT_USER. As from R 2.11.0 the same entries are also created under Software\R-core\R32 or Software\R-core\R64, for 32- and 64-bit R respectively.

If you tick the second option (shown with administrative privileges only) (`Associate R with .RData files') then entries are created under HKEY_CLASSES_ROOT\.RData and HKEY_CLASSES_ROOT\RWorkspace.

After installation you can add the Registry entries in by running RSetReg.exe in the bin folder, and remove them by running this with argument /U. Note that this requires administrative privileges unless run with argument /Personal and neither sets up nor removes the file associations. 

the following code lines in the windows-command-line outputs the R installation path:

reg query "HKLM\Software\R-core\R" /v "InstallPath"

or

reg query "HKCU\Software\R-core\R" /v "InstallPath"

in the following way:

C:\data\private\compiling_gis\detect_R_in_Dos>reg query "HKLM\Software\R-core\R" /v "InstallPath"

HKEY_LOCAL_MACHINE\Software\R-core\R
    InstallPath    REG_SZ    C:\Program Files\R\R-2.11.1

following line of code extract from the above output the R-installation path and set a variable which could be added to %PATH%

set RegCommand=reg query "HKLM\Software\R-core\R" /v "InstallPath"
FOR /f "tokens=1-2* delims= " %%a IN ( ' %RegCommand% ^| find "InstallPath" ' ) do set R_HKLM_INSTALL_PATH=%%c

or

set RegCommand2=reg query "HKCU\Software\R-core\R" /v "InstallPath"
FOR /f "tokens=1-2* delims= " %%a IN ( ' %RegCommand2% ^| find "InstallPath" ' ) do set R_HKCU_INSTALL_PATH=%%c

but if there is no value in registry, following error comes up:

C:\data\private\compiling_gis\detect_R_in_Dos>FOR /F "tokens=1-2* delims= " %a IN (' reg query "HKCU\Software\R-core\R" /v "InstallPath" | find "InstallPath" '
) do set R_HKCU_INSTALL_PATH=%c
ERROR: registry key or value couldn't be found.

following questions:

  • which of in the R-Windows-FAQ mentioned registry entries should be tested?
  • how could the above mentioned lines of code be added to grassXX.bad? (I'm not really familiar with this kind of coding)

this would be nice in order to have a R-session working in the Grass-delivered command line, independently if the R-installation has changed.

best regards Helmut

Attachments (5)

dynamic_test_if_R_is_installed.bat (1.9 KB ) - added by hellik 14 years ago.
Dynamic check where R lives and added to path
grass64svn_add_dynamically_R_path.patch (3.4 KB ) - added by hellik 14 years ago.
Add dynamically R to Path for WinGrass64svn
grass65_add_dynamically_R_path.patch (3.4 KB ) - added by hellik 14 years ago.
Add dynamically R to Path for WinGrass65
grass70_add_dynamically_R_path.patch (3.4 KB ) - added by hellik 14 years ago.
Add dynamically R to Path for WinGrass70
README_rbatch.txt (14.3 KB ) - added by hellik 12 years ago.
README r batch

Download all attachments as: .zip

Change History (17)

in reply to:  description ; comment:1 by glynn, 14 years ago

Replying to hellik:

  • which of in the R-Windows-FAQ mentioned registry entries should be tested?

HKCU first, then HKLM if nothing found under HKCU.

in reply to:  1 ; comment:2 by hellik, 14 years ago

Replying to glynn:

Replying to hellik:

  • which of in the R-Windows-FAQ mentioned registry entries should be tested?

HKCU first, then HKLM if nothing found under HKCU.

I have R 2.12.0 installed in C:\Program Files\R\R-2.12.0.

according to the R-Windows-FAQ, the R-binaries are installed

in a Win32-installation: C:\Program Files\R\R-2.12.0\bin\i386\

in a Win64-installation: C:\Program Files\R\R-2.12.0\bin\x64\

If I write following search-routine in a bat-file (for example c:\temp\addrtopath.bat) for all possible cases

set RegCommandHKCU32=reg query "HKCU\Software\R-core\R32" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKCU32% ^| find "InstallPath"') do set R_HKCU32_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKCU32_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKCU32_INSTALL_PATH%\bin\i386) >nul 2>&1
set RegCommandHKCU64=reg query "HKCU\Software\R-core\R64" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKCU64% ^| find "InstallPath"') do set R_HKCU64_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKCU64_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKCU64_INSTALL_PATH%\bin\x64) >nul 2>&1
rem Test if R is registered in HKLM
set RegCommandHKLM32=reg query "HKLM\Software\R-core\R32" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKLM32% ^| find "InstallPath"') do set R_HKLM32_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKLM32_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKLM32_INSTALL_PATH%\bin\i386) >nul 2>&1
set RegCommandHKLM64=reg query "HKLM\Software\R-core\R64" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKLM64% ^| find "InstallPath"') do set R_HKLM64_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKLM64_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKLM64_INSTALL_PATH%\bin\x64) >nul 2>&1

and start that bat-file on my winvista32-box in a normal windows-cmd, the R-installation is found correctly and in my case C:\Program Files\R\R-2.12.0\bin\i386\ is added to path and I can start R by typing R in the windows-cmd where I have run the bat-file.

If I add these line of code above into C:\Program Files\GRASS-70-SVN\grass70svn.bat (Wingrass70-starting script), R isn't added to the path.

any hints?

best regards Helmut

by hellik, 14 years ago

Dynamic check where R lives and added to path

in reply to:  2 ; comment:3 by hellik, 14 years ago

Replying to hellik:

Replying to glynn:

set RegCommandHKCU32=reg query "HKCU\Software\R-core\R32" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKCU32% ^| find "InstallPath"') do set R_HKCU32_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKCU32_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKCU32_INSTALL_PATH%\bin\i386) >nul 2>&1
set RegCommandHKCU64=reg query "HKCU\Software\R-core\R64" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKCU64% ^| find "InstallPath"') do set R_HKCU64_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKCU64_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKCU64_INSTALL_PATH%\bin\x64) >nul 2>&1
rem Test if R is registered in HKLM
set RegCommandHKLM32=reg query "HKLM\Software\R-core\R32" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKLM32% ^| find "InstallPath"') do set R_HKLM32_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKLM32_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKLM32_INSTALL_PATH%\bin\i386) >nul 2>&1
set RegCommandHKLM64=reg query "HKLM\Software\R-core\R64" /v "InstallPath" >nul 2>&1
FOR /f "tokens=1-2* delims= " %%a IN ('%RegCommandHKLM64% ^| find "InstallPath"') do set R_HKLM64_INSTALL_PATH=%%c >nul 2>&1
IF "%R_HKLM64_INSTALL_PATH%" NEQ "" (PATH=%PATH%;%R_HKLM64_INSTALL_PATH%\bin\x64) >nul 2>&1

sorry above isn't really working.

I've attached instead a simple working bat-file to check dynamically if and where R is installed and added to the path.

but if these lines are added to grass70svn.bat, they are not working correctly.

any hints what have to be changed?

Helmut

by hellik, 14 years ago

Add dynamically R to Path for WinGrass64svn

by hellik, 14 years ago

Add dynamically R to Path for WinGrass65

in reply to:  3 ; comment:4 by hellik, 14 years ago

Replying to hellik:

I've attached instead a simple working bat-file to check dynamically if and where R is installed and added to the path.

I've attached working patches for WinGrass64svn and WinGrass65.

tested with a patched WinGrass6.4.1RC2, it's working now. R can be started within a WinGrass-text-mode-session.

the improvement is that R is added to %PATH% dynamically instead of the actually very static approach which detects R only during wingrass-installation and, after installing new R-versions, it's not possible anymore to start a R-session within a wingrass-session because of the false path.

any opinion for not adding it to grass65 and after some testing to grass64svn?

Helmut

by hellik, 14 years ago

Add dynamically R to Path for WinGrass70

in reply to:  4 ; comment:5 by hellik, 14 years ago

Replying to hellik:

I've attached working patches for WinGrass64svn and WinGrass65.

also now attached a working tested patch for WinGrass70.

after some cleaning of the messages, it would be nice to have it in all branches.

I'm often coupling R and grass inside a grass-session in windows, it's a very nice feature for analysing gis-data to have.

Helmut

in reply to:  5 ; comment:6 by hellik, 14 years ago

Replying to hellik:

Replying to hellik:

I've attached working patches for WinGrass64svn and WinGrass65.

also now attached a working tested patch for WinGrass70.

after some cleaning of the messages, it would be nice to have it in all branches.

I'm often coupling R and grass inside a grass-session in windows, it's a very nice feature for analysing gis-data to have.

see also Windows batchfiles for use with R

in reply to:  6 ; comment:7 by hellik, 14 years ago

Replying to hellik:

Replying to hellik:

Replying to hellik:

I've attached working patches for WinGrass64svn and WinGrass65.

also now attached a working tested patch for WinGrass70.

after some cleaning of the messages, it would be nice to have it in all branches.

I'm often coupling R and grass inside a grass-session in windows, it's a very nice feature for analysing gis-data to have.

see also Windows batchfiles for use with R

I've tested following in my win32-vista-box

  • installed nightly build WinGRASS-7.0.SVN-r46330-1-Setup.exe
  • started "GRASS 7.0.SVN with MSYS" by the desktop-shortcut
  • in the windows-command-line of "GRASS 7.0.SVN with MSYS" it's possible to start a R-session within a Grass-session (recognizing all Grass-environment-variables) by typing cmd R.bat
  • in the windows-command-line of "GRASS 7.0.SVN with MSYS" it's possible to start a R-GUI-session within a Grass-session (recognizing all Grass-environment-variables) by typing cmd Rgui.bat

so I strongly recommend using the bat-files from http://code.google.com/p/batchfiles/ for a smoothly WinGrass-R-integration.

the bat-files are gpl'ed, so there's maybe no problem to include them in an wingrass-selfinstaller?

after some tweaking (i.e. starting from the wxgui, ...), this would be a great and an all-time improvement coupling wonderfull GIS- and (geo-)statistic- software in the windows environment.

Helmut

in reply to:  7 comment:8 by hellik, 14 years ago

Replying to hellik:

Replying to hellik:

Replying to hellik:

Replying to hellik:

I've attached working patches for WinGrass64svn and WinGrass65.

also now attached a working tested patch for WinGrass70.

after some cleaning of the messages, it would be nice to have it in all branches.

I'm often coupling R and grass inside a grass-session in windows, it's a very nice feature for analysing gis-data to have.

see also Windows batchfiles for use with R

I've tested following in my win32-vista-box

  • installed nightly build WinGRASS-7.0.SVN-r46330-1-Setup.exe
  • started "GRASS 7.0.SVN with MSYS" by the desktop-shortcut
  • in the windows-command-line of "GRASS 7.0.SVN with MSYS" it's possible to start a R-session within a Grass-session (recognizing all Grass-environment-variables) by typing cmd R.bat
  • in the windows-command-line of "GRASS 7.0.SVN with MSYS" it's possible to start a R-GUI-session within a Grass-session (recognizing all Grass-environment-variables) by typing cmd Rgui.bat

so I strongly recommend using the bat-files from http://code.google.com/p/batchfiles/ for a smoothly WinGrass-R-integration.

the bat-files are gpl'ed, so there's maybe no problem to include them in an wingrass-selfinstaller?

after some tweaking (i.e. starting from the wxgui, ...), this would be a great and an all-time improvement coupling wonderfull GIS- and (geo-)statistic- software in the windows environment.

a small correction

  • start "GRASS 7.0.SVN with MSYS"
  • type in the command-line-window cmd for enabling a windows-command-line
  • type R.bat for starting a R-commandline-session within a wingrass-session or type Rgui.bat for starting a R-GUI-session within a winggrass-session (in both cases recognizing all Grass-environment-variables)

Helmut

in reply to:  7 ; comment:9 by hellik, 12 years ago

Replying to hellik:

so I strongly recommend using the bat-files from http://code.google.com/p/batchfiles/ for a smoothly WinGrass-R-integration.

the bat-files are gpl'ed, so there's maybe no problem to include them in an wingrass-selfinstaller?

answer from Gabor Grothendieck, author of "Windows batchfiles for use with R".

Von: Gabor Grothendieck <...>
Betreff: Re: WinGRASS GIS-R-integration via "Windows batchfiles for use with R"
Datum:	Thu, 23. Aug 2012 23:06:30

Hi, If the entire project is GPL'd or subject to reasonably similar
license(s) then there is no reason you could not distribute additional
GPL'd software like the batchfiles with it.  I think the license only
requires preservation of authorship and maintaining the free status.
Regards.

the batch-files are:

PROGRAM LIST

Legend:
h = no args gives help
0 = common usage is to enter command name without arguments
d = in development
2.11 = only works with R 2.11
* = all files marked with one star are the same.  Program checks name by which its called to determine action.
** = all files marked with two stars are the same.  Program checks name by which its called to determine action.

#Rscript.bat - put at top of R file to make it a batch file (h) (*)
clip2r.js - pastes clipboard into Rgui.  See comments in file for use from vim. (0)(d)
copydir.bat - copy a library from one version of R to another (h)
el.js - run elevated - Vista and up, e.g. el Rgui  runs R elevated
find-miktex.hta - GUI to find MiKTeX (0)
kopy.bat - copy Rcmd to other batch files (h)(d)
movedir.bat - move library from one version of R to another (h) 
R.bat - like R.exe but finds R from registry (0) (*)
Rcmd.bat - like Rcmd.exe but finds R from registry (h) (*)
Rgui.bat - like Rgui.exe but finds R from registry (0) (*)
RguiStart.bat - like Rgui.bat but arg1 defines folder to start R in (*)
Rscript.bat - run .R script (h) (*)
Rterm.bat - like rterm.exe but finds R from registry (h) (*)
Rtidy.bat - reformat a .R file, e.g. Rtidy myfile.R > outfile.R (d)
Rtools.bat - place Rtools on path for remainder of console session (0) (*)
Rversions.bat - list R and set R version in registry, e.g. on Vista: el cmd/c Rversions R-2.10.1 (0)
Rversions.hta - GUI interface to RSetReg.  List and set R version, e.g. on Vista: el cmd /c Rversions.hta (0)(2.11)
show-svn-info.hta - show svn info if current folder is an svn checkout (0)
Stangle.bat - run arg1 through Stangle (h) (**)
Sweave.bat - run arg1 through Sweave (h) (**)

the entire README of rbatch-files attached to this ticket.

so may I propose to integrate the batch files in the GRASS GIS source in

\src\grass_trunk\mswindows\external\rbatch

and the standalone WinGrass-installer installing the files in

C:\Program Files (x86)\GRASS GIS 7.0.svn\extrabin

tested here locally, it's working.

this would be a great and an all-time improvement coupling wonderfull GIS- and (geo-)statistic- software in the windows world.

any opinions, objections, hints ...?

Helmut

by hellik, 12 years ago

Attachment: README_rbatch.txt added

README r batch

in reply to:  9 ; comment:10 by hellik, 12 years ago

Replying to hellik:

any opinions, objections, hints ...?

done in

grass trunk:

r53032 r53033 r53034

grass devbr6:

r53035 r53036

...testing welcome

Helmut

in reply to:  10 comment:11 by hellik, 12 years ago

Replying to hellik:

Replying to hellik:

any opinions, objections, hints ...?

done in

grass trunk:

r53032 r53033 r53034

grass devbr6:

r53035 r53036

...testing welcome

wiki updated:

http://grass.osgeo.org/wiki/R_statistics#Usage_III

Helmut

in reply to:  10 comment:12 by hellik, 12 years ago

Resolution: fixed
Status: newclosed

Replying to hellik:

done in

grass trunk:

r53032 r53033 r53034

grass devbr6:

r53035 r53036

closing ticket.

Note: See TracTickets for help on using tickets.