[英]QGIS: How to run R directly in python console?
I have been using QGIS python console to automate my needs. 我一直在使用QGIS python控制台自动化我的需求。 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.
我已经使用了一些处理算法(例如距离矩阵)来处理输出csv文件的向量层,在将它们作为变量返回到python控制台之前,我需要R对这些文件进行处理。
Is there a way I can run R directly through the python console (maybe using packages such as rpy2?) 有没有一种方法可以直接通过python控制台运行R(也许使用诸如rpy2之类的软件包?)
I guess you can easily interact with a R instance in the QGis python console using rpy2
. 我猜您可以使用
rpy2
在QGis python控制台中轻松与R实例进行rpy2
。 Try these following lines of code in the QGIS python console: 在QGIS python控制台中尝试以下几行代码:
>>> 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 : 您现在可以像这样与R交互:
>>> 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). 看一下Rpy2的文档 ,我已经成功地使用了这些方法来运行从CRAN安装的一些个人脚本或某些库(在
robjects.r("""...""")
运行多个语句并获取输出)在要在QGIS中使用的python变量中)。
(If i remember well, on windows I had to set some environnement variables first, like R_HOME
or R_USER
maybe) (如果我记得很好,在Windows上我必须先设置一些环境变量,例如
R_HOME
或R_USER
)
Also, if you haven't seen it, take a look at this page of the QGIS documentation : 17.31. 另外,如果您还没有看到它,请看一下QGIS文档的此页面: 17.31。 Use R scripts in Processing .
在Processing中使用R脚本 。 It offers a convenient way to use your existing R script with some slight add.
它提供了一种使用现有R脚本的便捷方法,只需稍加添加即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.