We’ll be working with the latter API first. Using the Open Notify API, we can learn about the location of the International Space Station and how many people are currently in space. For our example, we’ll be working with the Open Notify API, which opens up data on various NASA projects. The GET() function encapsulates all of the complexity of a GET request. The GET() function requires a URL, which specifies the address of the server that the request needs to be sent to. In order to create a GET request, we need to use the GET() function from the httr library. Other types of requests are POST and PUT, but we won’t need to worry about them for the purposes of this data-science-focused R API tutorial. These types of requests correspond to different actions that you want the server to make.įor our purposes, we’ll just be asking for data, which corresponds to a GET request. There are several types of requests that one can make to an API server. This request will be sent to the computer server that has the API, and assuming everything goes smoothly, it will send back a response. The first step in getting data from an API is making the actual request in R. Library(jsonlite) Making Our First API Request install.packages(c("httr", "jsonlite"))Īfter downloading the libraries, we’ll be able to use them in our R scripts or RMarkdown files. Use the install.packages() function to bring in these packages. If you don’t have either of these libraries in your R console or RStudio, you’ll need to download them first. ![]() They serve different roles in our introduction of APIs, but both are essential. The R libraries that we’ll be using are httr and jsonlite. These libraries take all of the complexities of an API request and wrap them up in functions that we can use in single lines of code. To work with APIs in R, we need to bring in some libraries. Often, we can immediately use the data we get from an API, which saves us time and frustration. While there are certainly libraries out there that make parsing HTML text easy, these are all cleaning steps that need to be taken before we even get our hands on the data we want! When a programmer scrapes a web page, they receive the data in a messy chunk of HTML. Why is this valuable? Contrast the API approach to pure web scraping. That computer will then read our code, process the request, and return nicely-formatted data that can be easily parsed by existing R libraries. From our perspective as the requester, we will need to write code in R that creates the request and tells the computer running the API what we need. Once this computer receives a data request, it will do its own processing of the data and send it to the computer that requested it. When a website like Facebook sets up an API, they are essentially setting up a computer that waits for data requests. In this tutorial, we’ll specifically be working with web APIs, where two different computers - a client and server - will interact with each other to request and provide data, respectively.ĪPIs offer data scientists a polished way to request clean and curated data from a website. ![]() “API” is a general term for the place where one computer program interacts with another, or with itself. ![]() Start learning R today with our Introduction to R course - no credit card required! SIGN UPĪlready have the R programming skills you need? Great! Let’s dive into accessing APIs with R.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |