<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/"></div></div><div><a data-url="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/" data-counturl="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/" data-count="horizontal" data-text="Noob package development: job ads data in R with Adzuna" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmnppt.com/2016/06/08/adzunar-package-blog.html"> Adventures in Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Part of our mission at The Data Lab is to create jobs in Data Science. This got us thinking that perhaps we should find a systematic way of quantifying the job market in Scotland and generally. We stumbled upon Adzuna, never heard of it? It’s quite cool, looks and feels like a jobs board but is actually a very data enabled service. For example you can upload your CV and it will leverage its database of job ads to predict your salary worth. I still have not had the guts to upload my own <img alt=":-)" class="wp-smiley" src="http://i2.wp.com/www.r-bloggers.com/wp-includes/images/smilies/simple-smile.png?w=456" data-recalc-dims="1"></p>
<p>Anyway the good news for us is that Adzuna has a nice REST API that we can query and get results for Scotland or any geographical area. One of my short term goals was to learn more about packages in R and so I decided this was a good opportunity to develop a lightweight R package. First of all go and read <a target="_blank" href="https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/">this blog</a> by <a target="_blank" href="https://twitter.com/hspter">Hilary Parker</a> which was an absolutely perfect distillation of the process. In fact, this page is a shameless re-hash of Hilary’s page with my own function. Unless you’re interested in getting job data or data from other REST API’s then go and read her page instead.</p>
<p>Before you start you need to install a few packages.</p>
<div>
<pre><code><span>install.packages</span><span>(</span><span>&quot;devtools&quot;</span><span>)</span>
<span>library</span><span>(</span><span>&quot;devtools&quot;</span><span>)</span>
<span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;klutometis/roxygen&quot;</span><span>)</span>
<span>library</span><span>(</span><span>roxygen2</span><span>)</span>
</code></pre>
</div>
<p>First step is to create the package directory in R studio.</p>
<div>
<pre><code><span>setwd</span><span>(</span><span>&quot;parent_directory&quot;</span><span>)</span>
<span>create</span><span>(</span><span>&quot;adzunar&quot;</span><span>)</span>
</code></pre>
</div>
<p>This will create the directory and some basic template files for your new package. Now its time to add some functions, in my case, I wanted to have a function that would help me query the Adzuna API. You can read the documentation for their API <a target="_blank" href="https://developer.adzuna.com/">here</a>. First of all what does a typical REST API call look like?</p>
<div>
<pre><code><span>http</span><span>://</span><span>api.adzuna.com</span><span>/</span><span>v1</span><span>/</span><span>api</span><span>/</span><span>property</span><span>/</span><span>gb</span><span>/</span><span>search</span><span>/</span><span>1</span><span>?</span><span>app_id</span><span>=</span><span>{</span><span>YOUR_APP_ID</span><span>}</span><span>&amp;</span><span>app_key</span><span>=</span><span>{</span><span>YOUR_APP_KEY</span><span>}</span>
</code></pre>
</div>
<p>As you can see, it is simply a url. The url contains the base location of the assets <code>api.adzuna.com</code>, the geographic location of interest <code>gb</code> and after the final <code>/</code> some further arguments, a minumum of your api key and app id (obtained from the sample place as the API documentation).</p>
<p>So all we would need to do in R is construct this url pasting in the values of our arguments of choice. Once we have this we can use a very handy function from <code>library(jsonlite)</code> that will submit the API call, retrieve the json data object and convert it to a data.frame. Wow! Tip of the hat to the author of <code>library(jsonlite)</code> <a target="_blank" href="https://twitter.com/opencpu">Jeroen Ooms</a>.</p>
<p>I wrote this function as follows;</p>
<div>
<pre><code><span>get_country_page</span> <span>&lt;-</span> <span>function</span><span>(</span>
  <span>keyword</span><span>,</span> 
  <span>country</span><span>,</span> 
  <span>app_id</span><span>,</span> 
  <span>app_key</span><span>,</span> 
  <span>page</span>
<span>)</span> <span>{</span>
  
  <span>this_url</span> <span>&lt;-</span> <span>paste0</span><span>(</span>
    <span>&quot;http://api.adzuna.com:80/v1/api/jobs/&quot;</span><span>,</span>
    <span>country</span><span>,</span>
    <span>&quot;/search/&quot;</span><span>,</span>
    <span>page</span><span>,</span> <span>&quot;?&quot;</span><span>,</span>
    <span>&quot;app_id=&quot;</span><span>,</span> <span>app_id</span><span>,</span>
    <span>&quot;&amp;app_key=&quot;</span><span>,</span> <span>app_key</span><span>,</span>
    <span>&quot;&amp;results_per_page=50&quot;</span><span>,</span>
    <span>&quot;&amp;what=&quot;</span><span>,</span> <span>keyword</span>
  <span>)</span>
  
  <span>dat</span> <span>&lt;-</span> <span>fromJSON</span><span>(</span><span>this_url</span><span>)</span>
  
  <span>return</span><span>(</span><span>dat</span><span>)</span>
  
<span>}</span>
</code></pre>
</div>
<p>All thats happening here is the construction of that url for the api call, passing it to <code>fromJSON()</code> and then returning the result. In this case the i’m allowing for some search terms, a different geographic location, the app key and id and finally the page. If you imagine that the web front end returns results in pages that you navigate through, the API is the same, you need to request a particular page of the results. In reality I have added some more functionality to this, which you can examine at the github repo for <a target="_blank" href="https://github.com/rmnppt/adzunar">adzunar</a>.</p>
<p>Once you have your function, its then really easy to write all of the documentation. What you do is write special comments at the top of the file for the function, then <code>library(roxygen2)</code> will compile it into the help file for that function. Below is an example for the function I wrote.</p>
<div>
<pre><code><span>#' Function to query the API by keyword country and results page.
#'
#' This function allows you to query the adzuna API, specifying a keyword, a country code and the number of results that you want. The API limit is 50 per page but if you specify more than that this function will continue to run your query, request succesive pages of the results and return the aggregate data object as a `data.frame`. You can request results that exceed the maximum returned by the API.
#' @param keyword A search string (required)
#' @param country A two letter country code. Any one of &quot;gb&quot;, &quot;au&quot;, &quot;br&quot;, &quot;ca&quot;, &quot;de&quot;, &quot;fr&quot;, &quot;in&quot;, &quot;nl&quot;, &quot;pl&quot;, &quot;ru&quot;, &quot;za&quot;. Defaults to &quot;gb&quot;.
#' @param app_id Your app id provided by Adzuna (required)
#' @param app_key Your app key provided by Adzuna (required)
#' @param n_results The number of results requested. Defaults to 50.
#' @keywords adzuna, API, data download, job adverts
#' @export
#' @examples
#' # (not run)
#' # id &lt;- [Your app id]
#' # key &lt;- [Your app key]
#' # get_country_page(&quot;data science&quot;, &quot;gb&quot;, id, key)
</span></code></pre>
</div>
<p>Now when you call the <code>document()</code> function from your console, this will be compiled into the help file for that function. The next step is to share your package. This can be easily done with github. I’m not going to provide much detail about this as this can be found elsewhere but for the basics you need four commands to share your package on github.</p>
<div>
<pre><code><span>git</span> <span>init</span>
<span>git</span> <span>add</span>
<span>git</span> <span>commit</span>
<span>git</span> <span>push</span>
</code></pre>
</div>
<p>Once your package lives on github all someone needs to do is install as follows.</p>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;rmnppt/adzunar&quot;</span><span>)</span>
<span>library</span><span>(</span><span>adzunar</span><span>)</span>
</code></pre>
</div>
<p>And away we are. now the user can start calling functions and importantly help files from the new package.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/"></div></div><div><a data-url="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/" data-counturl="http://www.r-bloggers.com/noob-package-development-job-ads-data-in-r-with-adzuna/" data-count="vertical" data-text="Noob package development: job ads data in R with Adzuna" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rmnppt.com/2016/06/08/adzunar-package-blog.html"> Adventures in Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/hhPpNTaKdIQ"></a></div>