将支持R代码的代码包装到python模块中? [英] Packaging supporting R code in a python module?
问题描述
我正在尝试使用 rpy2 打包一些调用R代码的Python代码.该R代码当前位于一个单独的文件中,该文件来自Python脚本,我为source
.例如,如果python脚本是myscript.py
,则R代码存储在myscript_support.R
中,而我在myscript.py
中有类似以下内容:
I am trying to package some of my Python code that calls R code using rpy2. That R code currently sits in a separate file which I source
from the Python script. For example, if the python script is myscript.py
, then the R code is stored in myscript_support.R
, and I have something like the following in myscript.py
:
from rpy2.robjects import *
# Load the R code
r.source(os.path.join(os.path.dirname(__file__), "myscript_support.R"))
# Call the R function
r[["myscript_R_function"]]()
我现在想使用setuptools打包此Python脚本,我有几个问题:
I now want to package this Python script using setuptools, and I have a few questions:
-
我应该如何打包R支持代码,一旦完成打包,如何找到R文件的路径以便我可以获取它呢?
How should I package the R support code, and once I have done so, how do I find the path to the R file so I can source it?
R代码取决于几个 R 软件包.如何确保已安装?如果无法加载这些R软件包,我是否应该提出一个提示性错误?
The R code depends on several R packages. How can I ensure that these are installed? Should I just raise an informative error if these R packages cannot be loaded?
推荐答案
这个问题可能已经过时,但是我今天遇到了同样的问题,想为@ivan_pozdeev建议的问题1解决方案和新解决方案提供更多详细信息问题2.
This question might be dated, but I ran into the same issue today and wanted to provide more detail for the question 1 solution suggested by @ivan_pozdeev and a new solution for question 2.
1)将setup.py文件编辑为:
1) Edit your setup.py file to:
from setuptools import setup, find_packages
setup(
...
# If any package contains *.r files, include them:
package_data={'': ['*.r', '*.R']},
include_package_data=True)
)
2)Conda迅速成为处理python和R程序包依赖关系的好选择.您可以创建一个环境( http://conda.pydata.org/docs/using/envs ),下载您可能需要的所有r和python程序包,然后生成一个environment.yml文件,以便任何人可以复制您的环境.请查看此博客以获取更多信息: https://www.continuum.io/content/conda -data-science
2) Conda is quickly becoming a good option for dealing with package dependencies across both python and R. You can create an environment (http://conda.pydata.org/docs/using/envs), download all the r and python packages that you might need, and then generate an environment.yml file so that anyone can replicate your environment. Check out this blog for more info: https://www.continuum.io/content/conda-data-science
这篇关于将支持R代码的代码包装到python模块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!