{"id":148384,"date":"2023-10-18T16:48:12","date_gmt":"2023-10-18T20:48:12","guid":{"rendered":"http:\/\/www.bu.edu\/tech\/?page_id=148384"},"modified":"2024-01-31T09:49:37","modified_gmt":"2024-01-31T14:49:37","slug":"r-py","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/special\/r-py\/","title":{"rendered":"R-Py"},"content":{"rendered":"<p><a name=\"top\"><\/a><br \/>\n<a href=\"https:\/\/www.python.org\/\">Python<\/a> is a popular all-purpose scripting language, while <a href=\"https:\/\/www.r-project.org\/\">R<\/a> is a scripting language mostly popular for data analysis, statistics, and graphics. If you are reading this, there are good chances that you are at least familiar with one or both. Having an interface between both languages to benefit from the libraries of one language while working in the other is desirable to many users. A number of tools have been developed to achieve this and a few popular options are detailed below. <\/p>\n<p><strong>Sections<\/strong><\/p>\n<ul>\n<li><a href=\"#r-in-py\">Access to R from within Python<\/a><\/li>\n<ul>\n<li><a href=\"#rpy2\">RPy2<\/a><\/li>\n<\/ul>\n<li><a href=\"#py-in-r\">Access to Python from within R<\/a><\/li>\n<ul>\n<li><a href=\"#reticulate\">Reticulate<\/a><\/li>\n<\/ul>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a name=\"r-in-py\"><\/a><\/p>\n<h2>Access to R from within Python<\/h2>\n<p><a name=\"RPy2\"><\/a><\/p>\n<h3><a href=\"https:\/\/rpy2.github.io\/doc\/latest\/html\/index.html\">RPy2<\/a><\/h3>\n<ol>\n<li>Load your desired R module:\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load R\/<\/span><span class=\"placeholder\">4.3.1<\/span><\/code><\/pre>\n<\/li>\n<li>Add the library path of the loaded R module to <b><code>LD_LIBARY_PATH<\/code><\/b>:\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">export LD_LIBRARY_PATH=$R_HOME\/lib:$LD_LIBRARY_PATH<\/span><\/code><\/pre>\n<\/li>\n<li> Create and activate your <a href=\"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/python-installs\/virtualenv\/\">virtualenv<\/a> or <a href=\"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/python-installs\/conda\/\">conda environment<\/a>.<\/li>\n<li>Install <a href=\"https:\/\/rpy2.github.io\/doc\/latest\/html\/overview.html#install-from-source\">RPy2<\/a> into your environment with <code><b>pip<\/b><\/code>:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my_newenv) [rcs@scc1 ~]<\/span> <span class=\"command\">pip install rpy2<\/span><\/code><\/pre>\n<\/li>\n<li>Import your R packages in Python:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my_newenv) [rcs@scc1 ~]<\/span> <span class=\"command\">python<\/span>\r\n<span class=\"prompt\">><\/span><span class=\"command\">from rpy2.robjects.packages import importr<\/span>\r\n\r\n<span class=\"placeholder\"># import R's \"base\" package<\/span>\r\n<span class=\"prompt\">><\/span><span class=\"command\">base = importr('base')<\/span><\/code><\/pre>\n<p>In essence, this is importing the R base package in the embedded R, and exposing all R objects in that package as Python objects. See the full <a href=\"https:\/\/rpy2.github.io\/doc\/latest\/html\/introduction.html#r-packages\">RPy2 documentation for R Packages<\/a>.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a name=\"py-in-r\"><\/a><\/p>\n<h2>Access to Python from within R<\/h2>\n<p><a name=\"reticulate\"><\/a><\/p>\n<h3><a href=\"https:\/\/rstudio.github.io\/reticulate\/\">Reticulate<\/a><\/h3>\n<ol>\n<li>Load your desired Python (or Miniconda) module:\n<pre><code class=\"code-block\"><span class=\"prompt\">[rcs@scc1 ~]<\/span> <span class=\"command\">module load python3\/<\/span><span class=\"placeholder\">3.10.12<\/span><\/code><\/pre>\n<\/li>\n<li> Create and activate your <a href=\"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/python-installs\/virtualenv\/\">virtualenv<\/a> or <a href=\"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/python\/python-installs\/conda\/\">conda environment<\/a>.<\/li>\n<li>Load your desired R module:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my_newenv) [rcs@scc1 ~]<\/span> <span class=\"command\">module load R\/<\/span><span class=\"placeholder\">4.3.1<\/span><\/code><\/pre>\n<\/li>\n<li>Install <a href=\"https:\/\/rstudio.github.io\/reticulate\/\">Reticulate<\/a> into R:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my_newenv) [rcs@scc1 ~]<\/span> <span class=\"command\">R<\/span>\r\n<span class=\"prompt\">><\/span><span class=\"command\">install.packages('reticulate')<\/span><\/code><\/pre>\n<\/li>\n<li>Load Reticulate in R and check that the correct Python environment is being used:\n<pre><code class=\"code-block\"><span class=\"prompt\">(my_newenv) [rcs@scc1 ~]<\/span> <span class=\"command\">R<\/span>\r\n<span class=\"prompt\">><\/span><span class=\"command\">library(reticulate)<\/span>\r\n<span class=\"prompt\">><\/span><span class=\"command\">py_config()<\/span>\r\npython:         \/projectnb\/<span class=\"placeholder\" style=\"color:blue\">rcs\/my_newenv<\/span>\/bin\/python\r\nlibpython:      \/share\/pkg.8\/python3\/3.10.12\/install\/lib\/libpython3.10.so\r\npythonhome:     \/projectnb\/<span class=\"placeholder\" style=\"color:blue\">rcs\/my_newenv<\/span>:\/projectnb\/<span class=\"placeholder\" style=\"color:blue\">rcs\/my_newenv<\/span>\r\nvirtualenv:     \/projectnb\/<span class=\"placeholder\" style=\"color:blue\">rcs\/my_newenv<\/span>\/bin\/activate_this.py\r\nversion:        3.10.12 (main, Jul 20 2023, 10:11:55) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)]\r\nnumpy:          \/projectnb\/<span class=\"placeholder\" style=\"color:blue\">rcs\/my_newenv<\/span>\/lib\/python3.10\/site-packages\/numpy\r\nnumpy_version:  1.26.2\r\n<\/code><\/pre>\n<p>See the full <a href=\"https:\/\/rstudio.github.io\/reticulate\/#python-version\">Reticulate documentation for loading Python environments<\/a>.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a href=\"#top\">Back to top<\/a><\/p>\n<p><script>\r\nconst date = new Date(document.lastModified);\r\ndocument.getElementById(\"demo\").innerHTML = date;\r\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a popular all-purpose scripting language, while R is a scripting language mostly popular for data analysis, statistics, and graphics. If you are reading this, there are good chances that you are at least familiar with one or both. Having an interface between both languages to benefit from the libraries of one language while&#8230;<\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":148380,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148384"}],"collection":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/comments?post=148384"}],"version-history":[{"count":13,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148384\/revisions"}],"predecessor-version":[{"id":150188,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148384\/revisions\/150188"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/148380"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/media?parent=148384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}