<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/reticulate-another-step-towards-a-multilingual-and-collaborative-way-of-working/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.eoda.de/2018/06/14/reticulate-another-step-towards-a-multilingual-and-collaborative-way-of-working/"> eoda english R news</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R, Julia, Python – todays data scientists have the choice between numerous different programming languages, each with their own strengths and weaknesses. Would it not be convenient to bring those languages together and use the individual strength of each language? The package “reticulate” for R takes a step in this direction.</p>
<p><img sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" src="https://i2.wp.com/blog.eoda.de/wp-content/uploads/2018/05/Example_plot_blog.png?w=450&ssl=1" alt="" class="wp-image-5066" data-recalc-dims="1"><span></span><br>
The reticulate package creates a Python connection for R which enables the user to combine both languages for an optimal result. This tutorial referring to the Python engine for R Markdown implemented by reticulate, we will illustrate the basic idea, functionality and usefulness of the engine.</p>
<p>As an in-depth tutorial with examples and a fictional use case, we include an HTML notebook which is created in R Markdown. You can find it <a rel="nofollow" href="https://www.eoda.de/reticulate_rmd.html" target="_blank">here</a>.</p>
<p><strong>The weapon of choice – data analysis in your day-to-day work</strong></p>
<p>It could all be so simple: Everyone speaks the same language, borders blur and collaborations become easier than ever. What seems to sound a bit dramatic, happens more often in the everyday life of a data scientist. Even though R can show its strengths from a statistical point of view, Python, as a multi-purpose language, is becoming increasingly popular in the data science scene. Because of its multifunctionality, it is used more and more frequently the programming language of choice. Therefore, it seems necessary to build a bridge between both languages and create a common space within a project.</p>
<p><strong>The virtual bridge – the reticulate package</strong></p>
<p>The reticulate package provides a Python connection for R. Users can execute Python code directly from the R console, load Python scripts into the R environment and make them usable for R. In addition, reticulate provides a Python engine for R Markdown, which is described in the following example in more detail.</p>
<p><strong>Introduction – setup &amp; Python chunks in R Markdown</strong></p>
<p>The setup of reticulate is very simple. After installing the package as well as the preferred distribution (2.7x or 3.x) you can</p>
<pre>library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)</pre>
<p>install the Python engine. The chunks of code work as usual and every R chunk can be used for a Python chunk.</p>
<p>For example:</p>
<pre>```{r testChunk_R, warning = FALSE, message = FALSE}
Code
```</pre>
<p>R chunk and</p>
<pre>```{python TestChunk_Py, warning = FALSE, message = FALSE}
Code
```</pre>
<p>a Python chunk. But attention: The code executed as a Python chunk will not be loaded into the global environment! Access to functions and/ or variables from outside is only possible when knit is executed which means that the execution of chunk alone is not enough. In our corresponding HTML notebook you will find further examples for the implementation of functions in different languages.</p>
<p><strong>Continuation – automatic type conversion and interconnectivity</strong></p>
<p>Taken together, reticulate creates an appropriate bridge between two languages. This allows developers with different programming skills to work in the same document of a joint project, all that without having to translate code. Furthermore, the strengths of both languages can be used. To loosen up boundaries, reticulate also provides the possibility to access variables of other languages within the chunk of one language given.<br>
The following code extract illustrates the function:</p>
<pre>```{r chunk1_R}
test_vector &lt;- c(1, 2, 4, 4, 7, 3, 5)
```
```{python chunk1_Py}
num_four = r.test_vector.count(4)
print „Der Vektor enthält “, num_four, „ mal die Zahl 4“
```
```{r chunk2_R}
out_string = paste(„Richtig, der Vektor enthält“, py$num_four, „mal die Zahl 4“)
print(out_string)
```</pre>
<p>Reticulate automatically adapts types of respective language. For example, if chunk1_Py reads test_vector, the R type vector is converted into the equivalent Python type list.<br>
Further type assignments can be found <a rel="nofollow" href="https://rstudio.github.io/reticulate/articles/introduction.html" target="_blank">here</a>.</p>
<p><strong>Source_Python – the “hidden treasure” among functions</strong></p>
<p>The ability to implement Python code directly into a document and use it across languages already provides us with a powerful tool for seamless collaboration. The hidden treasure among functions in the reticulate package can be found in the function source_python. This function reads a Python script and enables functions and variables for both languages within the markdown document. This operating mode is illustrated in the following example:</p>
<pre>// Datei bubblesort.py
def bsort(numbers):
	if(isinstance(numbers, list) == False or
	   all(isinstance(p, (int, float)) for p in numbers) == False or
	   len(numbers) == 0):
	    print &quot;Warning: Input must be of Type List, only numeric and have length >= 1&quot;
	    return
	for i in reversed(range(1, len(numbers))):
		for j in range(0, i):
			if(numbers[j] > numbers[j+1]):
				numbers = swap(numbers, j, j+1)
	return numbers
	
def swap(numbers, id1, id2):
	temp = numbers[id1]
	numbers[id1] = numbers[id2]
	numbers[id2] = temp
	return numbers

// Markdown Dokument
```{r setup_script}
source_python(„bubblesort.py“)
```
```{r sort_R}
bsort(c(4, 3, 2, 1))
```
Output: 1, 2, 3, 4
```{python sort_Py}
print bsort([4,3,2,1])
```</pre>
<p><strong>Output: 1,2,3,4</strong></p>
<p>The Python script bubblesort.py implements the bubblesort sorting algorithm under bsort(numbers) function. This implementation is then provided in the markdown document with source_python(„bubblesort.py“) and can be used by both languages, as shown above.</p>
<p>With this feature reticulate removes the last existing limitation. This allows developers to completely work in their preferred development environment and finally merge the results into a final document. Already existing Python scripts (e.g. scripts for database connection, deep learning algorithms, etc.) can be transferred seamlessly.</p>
<p><strong>Use case – text mining with preparatory work</strong></p>
<p>Considering the following fictional use case: an electronics retailer Elektro-X wants to sign an advertising contract with one of the three retailers Lenavu, Ace-Ar and AS-OS. In order to get a more detailed picture of the retailer’s satisfaction, Elektro-X starts a text mining project implemented in Python: shop reviews will be read and reviews for the retailer’s products will be searched. In addition, a function is provided for calculating an average rating of several reviews. Due to a lack of time, Elektro-X is unable to follow through with the project and hired eoda to finish this project. eoda has completed the analysis in R to gain access to ggplot for visualization while using the pre-work done in Python. eoda has transferred the project to R using the reticulate package. Furthermore, R has been used to visualize the evaluations and sentiment analysis.</p>
<p><strong>Conclusion – the future of big data</strong></p>
<p>Concluding, it can be said that packages like reticulate have the potential to significantly influence the big data industry and thus change its future. Especially service providers like eoda can benefit from barrier-free collaboration with their customers, as they can work on core problems more quickly without having to deal with possible problems beforehand. If you would like to work more efficiently on customer projects in the future, start considering language barriers solutions such as reticulate.</p>
<p><em>Would you like to know more about multilingual and collaborative working processes? We as data science specialists can help you take the next step. Contact us for more information at <a rel="nofollow" href="https://www.eoda.de/en/news.html" target="_blank">www.eoda.de</a>. </em></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.eoda.de/2018/06/14/reticulate-another-step-towards-a-multilingual-and-collaborative-way-of-working/"> eoda english R news</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/S8E6ZTscFXk" height="1"></a></div>