<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/"></div></div><div><a data-url="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/" data-counturl="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/" data-count="horizontal" data-text="ubeR: A Package for the Uber API" 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://www.exegetic.biz/blog/2016/08/uber-a-package-for-the-uber-api/"> R – Exegetic Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Uber exposes an extensive API for interacting with their service. <a target="_blank" href="https://github.com/DataWookie/ubeR">ubeR</a> is a R package for working with that API which Arthur Wu and I put together during a Hackathon at iXperience.</p>
<p><img alt="alt-text" src="https://cdn.rawgit.com/DataWookie/ubeR/master/uber-logo.svg"></p>
<h2>Installation</h2>
<p>The package is currently hosted on GitHub. Installation is simple using the <code>devtools</code> package.</p>
<pre>
> devtools::install_github(&quot;DataWookie/ubeR&quot;)
> library(ubeR)
</pre>
<h2>Authentication</h2>
<p>To work with the API you’ll need to <a target="_blank" href="https://developer.uber.com/dashboard/create">create a new application</a> for the Rides API.</p>
<ul>
<li> Set Redirect URL to http://localhost:1410/.
<li> Enable the <code>profile</code>, <code>places</code>, <code>ride_widgets</code>, <code>history_lite</code> and <code>history</code> scopes.
</ul>
<p>With the resulting Client ID and Client Secret you’ll be ready to authenticate. I’ve stored mine as environment variables but you can just hard code them into the script for starters.</p>
<pre>
> UBER_CLIENTID = Sys.getenv(&quot;UBER_CLIENTID&quot;)
> UBER_CLIENTSECRET = Sys.getenv(&quot;UBER_CLIENTSECRET&quot;)
> 
> uber_oauth(UBER_CLIENTID, UBER_CLIENTSECRET)
</pre>
<h2>Identity</h2>
<p>We can immediately use <code>uber_me()</code> to retrieve information about the authenticated user.</p>
<pre>
> identity &lt;- uber_me()
> names(identity)
[1] &quot;picture&quot;         &quot;first_name&quot;      &quot;last_name&quot;       &quot;uuid&quot;            &quot;rider_id&quot;       
[6] &quot;email&quot;           &quot;mobile_verified&quot; &quot;promo_code&quot;   
> identity$first_name
[1] &quot;Andrew&quot;
> identity$picture
[1] &quot;https://d1w2poirtb3as9.cloudfront.net/default.jpeg&quot;
</pre>
<p>Clearly I haven’t made enough effort in personalising my Uber account.</p>
<p><img src="https://i2.wp.com/d1w2poirtb3as9.cloudfront.net/default.jpeg?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Designated Places</h2>
<p>Uber allows you to specify predefined locations for “home” and “work”. These are accessible via <code>uber_places_get()</code>.</p>
<pre>
> uber_places_get(&quot;home&quot;)
$address
[1] &quot;St Andrews Dr, Durban North, 4051, South Africa&quot;

> uber_places_get(&quot;work&quot;)
$address
[1] &quot;Dock Rd, V &amp; A Waterfront, Cape Town, 8002, South Africa&quot;
</pre>
<p>These addresses can be modified using <code>uber_places_put()</code>.</p>
<h2>History</h2>
<p>You can access data for recent rides using <code>uber_history()</code>.</p>
<pre>
> history &lt;- uber_history(50, 0)
> names(history)
 [1] &quot;status&quot;       &quot;distance&quot;     &quot;request_time&quot; &quot;start_time&quot;   &quot;end_time&quot;     &quot;request_id&quot;  
 [7] &quot;product_id&quot;   &quot;latitude&quot;     &quot;display_name&quot; &quot;longitude&quot;
</pre>
<p>The response includes a wide range of fields, we’ll just pick out just a few of them for closer inspection.</p>
<pre>
> head(history)[, c(2, 4:5, 9)]
  distance          start_time            end_time  display_name
1   1.3140 2016-08-15 17:35:24 2016-08-15 17:48:54 New York City
2  13.6831 2016-08-11 15:29:58 2016-08-11 16:04:22     Cape Town
3   2.7314 2016-08-11 09:09:25 2016-08-11 09:23:51     Cape Town
4   3.2354 2016-08-10 19:28:41 2016-08-10 19:38:07     Cape Town
5   7.3413 2016-08-10 16:37:30 2016-08-10 17:21:16     Cape Town
6   4.3294 2016-08-10 13:38:49 2016-08-10 13:59:00     Cape Town
</pre>
<h2>Product Descriptions</h2>
<p>We can get a list of cars near to a specified location using <code>uber_products()</code>.</p>
<pre>
> cars &lt;- uber_products(latitude = -33.925278, longitude = 18.423889)
> names(cars)
[1] &quot;capacity&quot;          &quot;product_id&quot;        &quot;price_details&quot;     &quot;image&quot;            
[5] &quot;cash_enabled&quot;      &quot;shared&quot;            &quot;short_description&quot; &quot;display_name&quot;     
[9] &quot;description&quot;  
> cars[, c(1, 2, 7)]
  capacity                           product_id short_description
1        4 91901472-f30d-4614-8ba7-9fcc937cebf5             uberX
2        6 419f6bdc-7307-4ea8-9bb0-2c7d852b616a            uberXL
3        4 1dd39914-a689-4b27-a59d-a74e9be559a4         UberBLACK
</pre>
<p>Information for a particular car can also be accessed.</p>
<pre>
> product &lt;- uber_products(product_id = &quot;91901472-f30d-4614-8ba7-9fcc937cebf5&quot;)
> names(product)
[1] &quot;capacity&quot;          &quot;product_id&quot;        &quot;price_details&quot;     &quot;image&quot;            
[5] &quot;cash_enabled&quot;      &quot;shared&quot;            &quot;short_description&quot; &quot;display_name&quot;     
[9] &quot;description&quot;      
> product$price_details
$service_fees
list()

$cost_per_minute
[1] 0.7

$distance_unit
[1] &quot;km&quot;

$minimum
[1] 20

$cost_per_distance
[1] 7

$base
[1] 5

$cancellation_fee
[1] 25

$currency_code
[1] &quot;ZAR&quot;
</pre>
<h2>Estimates</h2>
<p>It’s good to have a rough idea of how much a ride is going to cost you. What about a trip from Mouille Point to the Old Biscuit Mill?</p>
<p><img sizes="(max-width: 800px) 100vw, 800px" alt="old-biscuit-mill" class="wp-image-4123" src="https://i1.wp.com/www.exegetic.biz/blog/wp-content/uploads/2016/08/old-biscuit-mill.png?resize=456%2C327" data-recalc-dims="1"></p>
<pre>
> estimate &lt;- uber_requests_estimate(start_latitude = -33.899656, start_longitude = 18.407663,
+                                    end_latitude = -33.927443, end_longitude = 18.457557)
> estimate$trip
$distance_unit
[1] &quot;mile&quot;

$duration_estimate
[1] 600

$distance_estimate
[1] 4.15

> estimate$pickup_estimate
[1] 4
> estimate$price
  high_amount display_amount display_name low_amount surge_multiplier currency_code
1        5.00           5.00    Base Fare       5.00                1           ZAR
2       56.12    42.15-56.12     Distance      42.15                1           ZAR
3        8.30      6.23-8.30         Time       6.23                1           ZAR
</pre>
<p>Not quite sure why the API is returning the distance in such obscure units. (Note to self: convert those to metric equivalent in next release!) The data above are based on the car nearest to the start location. What about prices for a selection of other cars?</p>
<pre>
> estimate &lt;- uber_estimate_price(start_latitude = -33.899656, start_longitude = 18.407663,
+                     end_latitude = -33.927443, end_longitude = 18.457557)
> names(estimate)
 [1] &quot;localized_display_name&quot; &quot;high_estimate&quot;          &quot;minimum&quot;                &quot;duration&quot;
 [5] &quot;estimate&quot;               &quot;distance&quot;               &quot;display_name&quot;           &quot;product_id&quot;
 [9] &quot;low_estimate&quot;           &quot;surge_multiplier&quot;       &quot;currency_code&quot;         
> estimate[, c(1, 5)]
  localized_display_name  estimate
1                  uberX  ZAR53-69
2                 uberXL  ZAR68-84
3              uberBLACK ZAR97-125
</pre>
<p>The time of arrival for each of those cars can be accessed via <code>uber_estimate_time()</code>.</p>
<pre>
> uber_estimate_time(start_latitude = -33.899656, start_longitude = 18.407663)
  localized_display_name estimate display_name                           product_id
1                  uberX      180        uberX 91901472-f30d-4614-8ba7-9fcc937cebf5
2                 uberXL      420       uberXL 419f6bdc-7307-4ea8-9bb0-2c7d852b616a
3              uberBLACK      300    uberBLACK 1dd39914-a689-4b27-a59d-a74e9be559a4
</pre>
<p>So, for example, the uberXL would be expected to arrive in 7 minutes, while the uberX would pick you up in only 3 minutes.</p>
<h2>Requesting a Ride</h2>
<p>It’s also possible to request a ride. At present these requests are directed to the <a target="_blank" href="https://developer.uber.com/docs/rides/sandbox">Uber API Sandbox</a>. After we have done further testing we’ll retarget the requests to the API proper.</p>
<p>A new ride is requested using <code>uber_requests()</code>.</p>
<pre>
> ride &lt;- uber_requests(start_address = &quot;37 Beach Road, Mouille Point, Cape Town&quot;,
+                       end_address = &quot;100 St Georges Mall, Cape Town City Centre, Cape Town&quot;)
</pre>
<p>Let’s find out the details of the result.</p>
<pre>
> names(ride)
 [1] &quot;status&quot;           &quot;destination&quot;      &quot;product_id&quot;       &quot;request_id&quot;
 [5] &quot;driver&quot;           &quot;pickup&quot;           &quot;eta&quot;              &quot;location&quot;
 [9] &quot;vehicle&quot;          &quot;surge_multiplier&quot; &quot;shared&quot;     
> ride$pickup
$latitude
[1] -33.9

$longitude
[1] 18.406
> ride$destination
$latitude
[1] -33.924

$longitude
[1] 18.42
</pre>
<p>Information about the currently requested ride can be accessed using <code>uber_requests_current()</code>. If we decide to walk instead, then it’s also possible to cancel the pickup.</p>
<pre>
> uber_requests_current_delete()
</pre>
<h2>Future</h2>
<p>For more information about units of measurement, limits and parameters of the Uber API, have a look at the <a target="_blank" href="https://developer.uber.com/docs/rides/api-overview">API Overview</a>.</p>
<p>We’ll be extending the package to cover the remaining API endpoints. But, for the moment, most of the core functionality is already covered.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.exegetic.biz/blog/2016/08/uber-a-package-for-the-uber-api/">ubeR: A Package for the Uber API</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.exegetic.biz/blog">Exegetic Analytics</a>.</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="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/"></div></div><div><a data-url="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/" data-counturl="https://www.r-bloggers.com/uber-a-package-for-the-uber-api/" data-count="vertical" data-text="ubeR: A Package for the Uber API" 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://www.exegetic.biz/blog/2016/08/uber-a-package-for-the-uber-api/"> R – Exegetic Analytics</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/G7fdQE_qwvo"></a></div>