2

I try to run R script via a python GUI.

Under R console, I installed the rgl package. In windows-7, R install packages under the current user folder, not the ProgramFiles\R... folder.

I have a simple demo.r script:

library('rgl')
example('plot3d')

If I run demo.r script using the newly installed rgl package with the following command line:

"c:\ProgramFiles\R\R..\bin\Rscript.exe" demo.r

It works perfectly!

Now, if I run it from python with the following script:

import subprocess
out = subprocess.check_output([r"c:\ProgramFiles\R\R..\bin\Rscript.exe", "demo.r"])

I get an error telling that rgl is not found/installed.

I checked that if my demo.r contains only a simple print to screen command, it works. This means that when launch from python, Rscript.exe do not know anymore where to find rgl package under the user folder.

If I copy the rgl package folder from the user folder to the ProgramFiles\R... folder, then it works.

Does anybody knows how I can specify make python running the R script and using the package from the user folder?

Thanks

Nilesh
  • 20,521
  • 16
  • 92
  • 148
user1520280
  • 665
  • 1
  • 6
  • 8

2 Answers2

0

It is better to create a lanucher.bat , I suupose

R_PATH :C:/Program Files/R/R-version

DEMO_PATH : your demo.r path

@echo off
C:
PATH R_PATH;%path%
cd DEMO_PATH
Rscript demo.R 
exit

You save launcher.bat in LAUNCHER_PATH

Now in python :

import subprocess
out = subprocess.check_output('LAUNCHER_PATH\launcher.bat')

PS : If you add some args to your python script it easy to add them to your launcher.bat , without changing your python code

 Rscript demo.R arg1 arg2
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Even via a bat file, I get the same problem. The rgl library is not found when launch from python – user1520280 Nov 30 '12 at 21:38
  • Yes, it's only when I do it from python. – user1520280 Dec 01 '12 at 10:36
  • I use Python 2.7.3 and R 2.15.2. – user1520280 Dec 01 '12 at 22:10
  • I mean , when you launch the .bat from your console what do you have like error? – agstudy Dec 01 '12 at 22:12
  • Trying to figure out what the problem is, I found that the Syst.getenv("R_LIBS_USER") generated by the Rprofile script does not return the same value is you run a R script from python via the DOS console or from the python idle. Its seems to me that as soon as you use a python script using TKinter (my GUI or idle) the R_LIBS_USER value in R is changed. In a console the value is "c:\users\myname\Documents\...." and via python/Tkinter, I get "c:\users\myname\....". Iguess that this is the reason why, the rgl library in not found. – user1520280 Dec 01 '12 at 22:18
  • try to add this Sys.setenv(R_LIBS_USER ='c:\users\myname\Documents\...') if the beginning of demo.R, or to change this variable in your widows envrionnements variables – agstudy Dec 01 '12 at 22:24
  • I tried to set the R_LIBS_USER and it did not work. To make it work, I had to add the R_LIBS_USER value to the libsPath with this: .libsPath(c('c:\users\myname\Documents\...', .libsPath())). Thanks for help. I still wonder why the R_USER value changes when launched from a python TK interface...mystery! – user1520280 Dec 04 '12 at 05:07
0

You need to set a R_USER environment variable in the subprocess, which R looks to when setting the .Library.site:

import subprocess, os
my_env = os.environ.copy()
my_env["R_USER"] =  my_env["HOMEPATH"]
subprocess.check_output([r"c:\ProgramFiles\R\R..\bin\Rscript.exe", "demo.r"], env=my_env)

Note that my_env["HOMEPATH"] works for me since that is where my \R\win-library\ directory resides. You may have to use something along the line of os.path.join(my_env["HOMEPATH"],"documents") depending on the location of your user library lives (See ?library in R for details). The same logic applies when launching R in a subprocess from any language (Powershell, Node, etc.)

Jthorpe
  • 9,756
  • 2
  • 49
  • 64