$30 off During Our Annual Pro Sale. View Details »

#ResBaz2019 Workshop - Accessing APIs using R

#ResBaz2019 Workshop - Accessing APIs using R

Open data on the internet is often made available, through Web service Application Programming Interfaces or APIs, by governments and other organisations. This workshop, will help you gain hands-on experience in requesting data from these different Web service APIs using R. Specifically, we will look into fetching weather and events data for Auckland region.

Manish Kukreja

July 12, 2019
Tweet

More Decks by Manish Kukreja

Other Decks in Education

Transcript

  1. ACCESSING APIS USING R Manish Kukreja https://datamak.ml #ResBaz2019 https://resbaz.auckland.ac.nz/schedule/#session-018

  2. BACKGROUND ON APIS • API stands for Application Programming Interface

    • GUI or CLI is for human-computer interaction • API is for program-program interaction • Some examples • Get data and plot it • Add a node to a computing cluster • Recognize faces as soon as an image is uploaded • APIs used on the web are mostly HTTP RESTful APIs or REST APIs
  3. REST API path to the endpoint http://example.com/api/v2/fetch/data URL to a

    domain
  4. HTTP VERBS • POST – used to create a record,

    data is passed using JSON or XML • GET – read single or list of records, response can be JSON or XML • PUT – update/replace single or entire list of records • PATCH – update/modify single or entire list of records • DELETE – delete the whole collection
  5. STEPS TO ACCESS API 1. Obtain the key for using

    the web service 2. Find the URL and endpoint path of the API 3. Convert input data to JSON format using toJSON() in the jsonlite package [optional] 4. Send input data to the API service using the appropriate HTTP VERB function in the httr package. Include API key in the function call. 5. Extract your results from the API response using the fromJSON() function
  6. JOIN THE WORKSPACE IN RSTUDIO.CLOUD https://tiny.cc/resbaz-api

  7. JOIN THE WORKSPACE IN RSTUDIO.CLOUD

  8. JOIN THE WORKSPACE IN RSTUDIO.CLOUD

  9. JOIN THE WORKSPACE IN RSTUDIO.CLOUD

  10. JOIN THE WORKSPACE IN RSTUDIO.CLOUD

  11. JOIN THE WORKSPACE IN RSTUDIO.CLOUD

  12. 1. OBTAIN THE SECRET KEY https://darksky.net/dev/register

  13. 2. FIND URL AND ENDPOINT PATH https://darksky.net/dev/docs#forecast-request

  14. 3. CONVERT INPUT DATA TO JSON SKIPPED

  15. 4. SEND REQUEST •Send GET request •Check response

  16. 5. EXTRACT RESULTS FROM RESPONSE •Use httr::content() to extract data

    from response object •Transform it for easy manipulation •Visualize, merge or analyse it
  17. WORKSHOP CODE • Darksky weather forecast API notebook • https://datamak.ml/projects/data-analysis/Darksky-API

    • Microsoft sentiment analysis API notebook • https://datamak.ml/projects/data-analysis/Sentiment-API
  18. THANK YOU