Interfacing with APIs using R: the basics

While R (and its package ecosystem) provides a wealth of functions for querying and analyzing data, in our cloud-enabled world there’s now a plethora of online services with APIs you can use to augment R’s capabilities. Many of these APIs use a RESTful interface, which means you will typically send/receive data encoded in the JSON format using HTTP commands.
Fortunately, as Steph Locke explains in her most recent R Quick tip, the process is pretty simple using R:

Obtain an authentication key for using the service 
Find the URL of the API service you wish to use
Convert your input data to JSON format using toJSON in the jsonlite package
Send your data to the API service using the POST function in the httr package. Include your API key using the add_headers function
Extract your results from the API response using the fromJSON function

Steph provides an example of detecting text language using the Microsoft Cognitive Services Text Analytics API, but that basic recipe should work for most APIs. (API keys for other Microsoft Cognitive Services APIs for vision, speech, language, knowledge and search are also available for free use with your Github account.) There many APIs available for use around the web, and this list of public APIs maintained by Abhishek Banthia is a useful resource.
Locke Data: R Quick tip: Microsoft Cognitive Services’ Text Analytics API

from Revolutions


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s