<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/wrapping-access-to-web-services-in-r-functions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://flovv.github.io/Accessing_a_web_api/"> Florian Teschner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>One of the great features of R is the possibility to quickly access web-services. While some companies have the habit and policy to document their APIs, there is still a large chunk of undocumented but great web-services that help the regular data scientist.</p>
<p>In the following short post, I will show how we can turn a simple web-serivce in a nice R-function.<br>
The example I am going to use is the linguee translation service: <a rel="nofollow" href="http://flovv.github.io/Accessing_a_web_api/www.deepl.com" target="_blank">DeepL</a>.<br>
Just as google translate, Deepl features a simple text field. When a user types in text, the translation appears in a second textbox. Users can choose between the languages.</p>
<p>In order to see how the service works in the backend, let’s have a quick look at the network traffic.<br>
For that we open the browser’s developer tools and jump to the network tab. Next, we type in a sentence and see which requests (XHR) are made. The interface repeatedly sends JSON requests to the following endpoint: “https://www.deepl.com/jsonrpc”.</p>
<p><img alt="Chrome's developer console" src="https://i2.wp.com/flovv.github.io/figures/api.PNG?w=456" data-recalc-dims="1"></p>
<p>Looking at a single request we can quickly identify the parameters that we typed in (grey area, in the lower right corner). We copy these in r and assign them to a variable.</p>
<figure>
<pre><code data-lang="r"><span>str</span><span> </span><span>&lt;-</span><span> </span><span>'{&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;method&quot;:&quot;LMT_handle_jobs&quot;,&quot;params&quot;:{&quot;jobs&quot;:[{&quot;kind&quot;:&quot;default&quot;,&quot;raw_en_sentence&quot;:&quot;R is awesome&quot;}],&quot;lang&quot;:{&quot;user_preferred_langs&quot;:[&quot;DE&quot;,&quot;EN&quot;,&quot;FR&quot;],&quot;source_lang_user_selected&quot;:&quot;auto&quot;,&quot;target_lang&quot;:&quot;FR&quot;},&quot;priority&quot;:-1},&quot;id&quot;:11}'</span></code></pre>
</figure>
<p>Using a service to format the json (e.g. https://jsonformatter.curiousconcept.com/) we can turn the blob in a well readable json file. Next, we convert the JSON string in a R object (a nested list) by using a simple JSON to R language translation:</p>
<figure>
<pre><code data-lang="r"><span>require</span><span>(</span><span>stringr</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>str1</span><span> </span><span>&lt;-</span><span> </span><span>str_replace_all</span><span>(</span><span>str</span><span>,</span><span> </span><span>&quot;:&quot;</span><span>,</span><span> </span><span>&quot;=&quot;</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>str2</span><span> </span><span>&lt;-</span><span> </span><span>str_replace_all</span><span>(</span><span>str1</span><span>,</span><span> </span><span>&quot;\\[&quot;</span><span>,</span><span> </span><span>&quot;list(&quot;</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>str3</span><span> </span><span>&lt;-</span><span> </span><span>str_replace_all</span><span>(</span><span>str2</span><span>,</span><span> </span><span>&quot;\\]&quot;</span><span>,</span><span> </span><span>&quot;)&quot;</span><span> </span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>str4</span><span> </span><span>&lt;-</span><span> </span><span>str_replace_all</span><span>(</span><span>str3</span><span>,</span><span> </span><span>&quot;\\{&quot;</span><span>,</span><span> </span><span>&quot;list(&quot;</span><span> </span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>str5</span><span> </span><span>&lt;-</span><span> </span><span>str_replace_all</span><span>(</span><span>str4</span><span>,</span><span> </span><span>&quot;\\}&quot;</span><span>,</span><span> </span><span>&quot;)&quot;</span><span> </span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""></span><span>eval</span><span>(</span><span>parse</span><span>(</span><span>text</span><span>=</span><span>str5</span><span>))</span></code></pre>
</figure>
<p>Finally, we evaluate the string as R-code, this gives us the DeepL web-services’ parameters as an R nested list.<br>
All we have to do now is wrap the parameters in a R function and use variables to change the important ones:</p>
<figure>
<pre><code data-lang="r"><span>require</span><span>(</span><span>rjson</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>require</span><span>(</span><span>httr</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""></span><span>deepLTranslate</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>text</span><span>=</span><span>&quot;R is awesome&quot;</span><span>,</span><span> </span><span>from_lang</span><span>=</span><span>&quot;EN&quot;</span><span>,</span><span> </span><span>to_lang</span><span>=</span><span>&quot;DE&quot;</span><span>){</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""> </span><span>BASE_URL</span><span> </span><span>=</span><span> </span><span>'https://www.deepl.com/jsonrpc'</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>JSONRPC_VERSION</span><span> </span><span>=</span><span> </span><span>'2.0'</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>DEEPL_METHOD</span><span> </span><span>=</span><span> </span><span>'LMT_handle_jobs'</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""> </span><span>params</span><span> </span><span>=</span><span>list</span><span>(</span><span>'jsonrpc'</span><span>=</span><span> </span><span>JSONRPC_VERSION</span><span>,</span><span> <br data-jekyll-commonmark-ghpages=""> </span><span>'method'</span><span>=</span><span> </span><span>DEEPL_METHOD</span><span>,</span><span> <br data-jekyll-commonmark-ghpages=""> </span><span>params</span><span>=</span><span> </span><span>list</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>'jobs'</span><span>=</span><span>list</span><span>(</span><span>list</span><span>(</span><span>'kind'</span><span>=</span><span> </span><span>&quot;defaut&quot;</span><span>,</span><span> </span><span>'raw_en_sentence'</span><span>=</span><span> </span><span>text</span><span>)),</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>'lang'</span><span>=</span><span>list</span><span>(</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>'user_preferred_langs'</span><span>=</span><span>list</span><span>(</span><span>from_lang</span><span>,</span><span>to_lang</span><span>),</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>'source_lang_user_selected'</span><span>=</span><span> </span><span>from_lang</span><span>,</span><span> </span><span>'target_lang'</span><span>=</span><span>to_lang</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>)</span><span> <br data-jekyll-commonmark-ghpages=""> </span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""> </span><span>res</span><span> </span><span>&lt;-</span><span> </span><span>POST</span><span>(</span><span>BASE_URL</span><span>,</span><span>body</span><span> </span><span>=</span><span> </span><span>toJSON</span><span>(</span><span>params</span><span>))</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""> </span><span>co</span><span> </span><span>&lt;-</span><span> </span><span>content</span><span>(</span><span>res</span><span>,</span><span> </span><span>&quot;text&quot;</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""> </span><span>if</span><span>(</span><span>res</span><span>$</span><span>status_code</span><span> </span><span>==</span><span>200</span><span>){</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>return</span><span>(</span><span>fromJSON</span><span>(</span><span>co</span><span>))</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>}</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>else</span><span>{</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>return</span><span>(</span><span>co</span><span>)</span><span><br data-jekyll-commonmark-ghpages=""> </span><span>}</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""></span><span>}</span><span><br data-jekyll-commonmark-ghpages=""> <br data-jekyll-commonmark-ghpages=""></span><span>#### excute the function with defaults ...</span><span><br data-jekyll-commonmark-ghpages=""></span><span>deepLTranslate</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## $id<br data-jekyll-commonmark-ghpages="">## [1] 0<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $jsonrpc<br data-jekyll-commonmark-ghpages="">## [1] &quot;2.0&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result<br data-jekyll-commonmark-ghpages="">## $result$source_lang<br data-jekyll-commonmark-ghpages="">## [1] &quot;EN&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$source_lang_is_confident<br data-jekyll-commonmark-ghpages="">## [1] 0<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$target_lang<br data-jekyll-commonmark-ghpages="">## [1] &quot;DE&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[1]]<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[1]]$num_symbols<br data-jekyll-commonmark-ghpages="">## [1] 5<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[1]]$postprocessed_sentence<br data-jekyll-commonmark-ghpages="">## [1] &quot;R ist fantastisch&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[1]]$score<br data-jekyll-commonmark-ghpages="">## [1] -5000.6<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[1]]$totalLogProb<br data-jekyll-commonmark-ghpages="">## [1] -4.37026<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[2]]<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[2]]$num_symbols<br data-jekyll-commonmark-ghpages="">## [1] 5<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[2]]$postprocessed_sentence<br data-jekyll-commonmark-ghpages="">## [1] &quot;R ist großartig&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[2]]$score<br data-jekyll-commonmark-ghpages="">## [1] -5000.64<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[2]]$totalLogProb<br data-jekyll-commonmark-ghpages="">## [1] -4.6875<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[3]]<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[3]]$num_symbols<br data-jekyll-commonmark-ghpages="">## [1] 6<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[3]]$postprocessed_sentence<br data-jekyll-commonmark-ghpages="">## [1] &quot;R ist fantastisch.&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[3]]$score<br data-jekyll-commonmark-ghpages="">## [1] -5000.67<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[3]]$totalLogProb<br data-jekyll-commonmark-ghpages="">## [1] -5.57148<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[4]]<br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[4]]$num_symbols<br data-jekyll-commonmark-ghpages="">## [1] 6<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[4]]$postprocessed_sentence<br data-jekyll-commonmark-ghpages="">## [1] &quot;R ist großartig.&quot;<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[4]]$score<br data-jekyll-commonmark-ghpages="">## [1] -5000.72<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$beams[[4]]$totalLogProb<br data-jekyll-commonmark-ghpages="">## [1] -6.03852<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$timeAfterPreprocessing<br data-jekyll-commonmark-ghpages="">## [1] 0<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$timeReceivedFromEndpoint<br data-jekyll-commonmark-ghpages="">## [1] 311<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$timeSentToEndpoint<br data-jekyll-commonmark-ghpages="">## [1] 0<br data-jekyll-commonmark-ghpages="">## <br data-jekyll-commonmark-ghpages="">## $result$translations[[1]]$total_time_endpoint<br data-jekyll-commonmark-ghpages="">## [1] 1</code></pre>
</figure>
<p>I hope the post helps you turn more web-services into R-functions/packages.<br>
If you are looking for other translation services have a look at the translate or translateR packages.</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="http://flovv.github.io/Accessing_a_web_api/"> Florian Teschner</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/6MuEkgy06T4" height="1"></a></div>