0

I have been using QGIS python console to automate my needs. I've used a few processing algorithms (such as distance matrix) to work on my vector layers which outputs csv files.I need R to work on these files before bringing them back to my python console as variables.

Is there a way I can run R directly through the python console (maybe using packages such as rpy2?)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • If you have an R script, you can run it as you would from a command line: http://stackoverflow.com/questions/18306362/run-r-script-from-command-line – Roman Luštrik Dec 31 '15 at 09:27
  • to run commands like Rscript, I have to be in R in the terminal/python console first, right? I am able to run R through my windows cmd just by typing 'r', but have been unable to do the same through the console in qgis... So how do i do it the same way as cmd? – Palash Siddamsettiwar Jan 04 '16 at 06:28
  • You can run Rscript as an external program ("Rscript yourscript.R"). See for example http://stackoverflow.com/questions/1811691/running-an-outside-program-executable-in-python – Roman Luštrik Jan 05 '16 at 08:58

1 Answers1

0

I guess you can easily interact with a R instance in the QGis python console using rpy2. Try these following lines of code in the QGIS python console:

>>> import rpy2.rinterface as rinterface
>>> rinterface.set_initoptions((b'rpy2', b'--no-save'))
>>> rinterface.initr()
0
>>> from rpy2.robjects.packages import importr
>>> import rpy2.robjects as robjects

You can now interact with R like this :

>>> robjects.r("""seq(1,12);""")
<IntVector - Python:0x7fa5f6e4abd8 / R:0x769f4a8>
[       1,        2,        3, ...,       10,       11,       12]

Or import some libraries for example :

>>> rutils = importr("utils")
>>> rgraphics = importr('graphics')

Take a look at the documentation of Rpy2, I have successfully used these methods to run some personals scripts or some libraries installed from the CRAN (running multiple statements in robjects.r("""...""") and grabbing the output in a python variable to use in QGIS).

(If i remember well, on windows I had to set some environnement variables first, like R_HOME or R_USER maybe)

Also, if you haven't seen it, take a look at this page of the QGIS documentation : 17.31. Use R scripts in Processing. It offers a convenient way to use your existing R script with some slight add.

mgc
  • 5,223
  • 1
  • 24
  • 37
  • I've installed rpy2 and it works from cmd.... i can import the package... But haven't been able to run the same through the QGIS python console- I tried- `import rpy2` or `import rpy2.rinterface as rinterface` It throws the error- _Traceback (most recent call last): File "", line 1, in File "C:/PROGRA~1/QGISPI~1/apps/qgis/./python\qgis\utils.py", line 478, in _import mod = _builtin_import(name, globals, locals, fromlist, level) ImportError: No module named rpy2_ – Palash Siddamsettiwar Jan 04 '16 at 10:25
  • Also, R Scripts won't do for the kind of application I'm looking for- too much back and forth between R and python – Palash Siddamsettiwar Jan 04 '16 at 10:29
  • ok, problem solved... just had to add the relevant folder for rpy2 in sys.path – Palash Siddamsettiwar Jan 04 '16 at 11:20