{"id":418,"date":"2016-12-16T11:52:33","date_gmt":"2016-12-16T10:52:33","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/?p=418"},"modified":"2016-12-16T12:07:49","modified_gmt":"2016-12-16T11:07:49","slug":"simple-example-on-using-rpython","status":"publish","type":"post","link":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/2016\/12\/16\/simple-example-on-using-rpython\/","title":{"rendered":"Using rPython to call Python in R"},"content":{"rendered":"<p><strong>A simple example.<\/strong><\/p>\n<p>Calling <em>Python<\/em> from within <em>R<\/em> via the R-package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/rPython\/index.html\" target=\"_blank\">rPython<\/a> is fairly easy. However, very little documentation exists on this package, and some of the commands may appear quirky at \ufb01rst. Also, don&#8217;t confuse <em>rPython<\/em> with <em>RPython<\/em> (see capitals)! The paucity of written documentation on this package seems to scare away many biologists, who \u2013 if they are like me \u2013 prefer to work with well-documented code. But fear not!<\/p>\n<p>Usage of <em>rPython<\/em> is straightforward with a bit of exercise. The following tutorial is intended to give a quick and simple example on how to work with <em>rPython<\/em>.<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code> &gt; library(rPython)<br \/>\nLoading required package: RJSONIO<br \/>\n<\/code><\/p>\n<\/div>\n<p>I use the command <em>python.exec()<\/em> to execute basically any Python operation within R.<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code># Generating a dictionary in Python<br \/>\n&gt; python.exec(\"capcities = {'sk':'Slovakia','de':'Germany',<br \/>\n'hu':'Hungary'}\")<br \/>\n<\/code><\/p>\n<\/div>\n<p>I use the command <em>python.get()<\/em> to assign a new variable in R with a value from Python.<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code># Pulling the dictionary from Python into R<br \/>\n&gt; abbrev_dict = python.get(\"capcities\")<br \/>\n&gt; abbrev_dict<br \/>\nsk\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 de\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hu<br \/>\n\"Slovakia\"\u00a0 \"Germany\"\u00a0 \"Hungary\"<br \/>\n<\/code><\/p>\n<\/div>\n<p>Note, however, that <em>python.get()<\/em> is very in\ufb02exible in what it can retrieve from Python.<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code>&gt; abbrevs = python.get(\"capcities.keys()\")<br \/>\nTraceback (most recent call last):<br \/>\n...<br \/>\nTypeError: dict_keys(['de', 'sk', 'hu']) is not JSON serializable<br \/>\n<\/code><\/p>\n<\/div>\n<p>Instead, I use:<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code>&gt; abbrevs = python.get(\"list(capcities.keys())\")<br \/>\n&gt; abbrevs<br \/>\n[1] \"sk\" \"de\" \"hu\"<br \/>\n<\/code><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A simple example. Calling Python from within R via the R-package rPython is fairly easy. However, very little documentation exists on this package, and some of the commands may appear quirky at \ufb01rst. Also, don&#8217;t confuse rPython with RPython (see capitals)! The paucity of written documentation on this package seems to scare away many biologists, [&hellip;]<\/p>\n","protected":false},"author":2306,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57598,805],"tags":[],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-bioinformatics","category-teaching"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/users\/2306"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":6,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/418\/revisions\/421"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}