library(httr)
library(tidyjson)
library(jsonlite)
library(tidyverse)
<- GET("http://api.open-notify.org/astros.json")
res
## Using jsonlite
<- res$content %>%
space rawToChar() %>%
fromJSON()
## Using tidyjson
<- res$content %>%
json_tbl rawToChar() %>%
as.tbl_json()
%>%
json_tbl spread_all() %>%
enter_object("people") %>%
gather_array() %>% # if it's a JSON array
spread_all() %>%
select(name)
## Using unnest_longer & unnest_wider
<- res$content %>%
space rawToChar() %>%
fromJSON()
list(space) %>%
tibble(data = .) %>%
unnest_wider(data) %>%
unnest_longer(people) %>%
unnest_wider(people) %>%
filter(craft == "ISS")
Application Programming Interfaces (APIs)
Now that we’ve talked about the JSON format for data files, it’s time we talked about one of the primary vehicles for them: APIs.
🎥 Required Video: What is an API?
This is the tip of the iceberg when it comes to APIs, but it will suffice to enable a lot of cool things for us. The video above used some nice examples like the restaurant and travel services, but there are APIs for accessing all kinds of data out there…like Facebook, Twitter, Google Maps, etc.
For our class we’re not interested in creating APIs, but mostly using them to access data that we otherwise couldn’t get our hands on…at least not as easily.
📖 Required Reading: R API Tutorial
The tutorial references the wrong link to access the API on the number of people in space. The correct link is: http://api.open-notify.org/astros.json
Check-in: APIs
- What two packages did the tutorial make use of?
- rvest and jsonlite
- httr and jsonlite
- httr and rvest
- httr and tidyjson
- XML and rvest
- What type of request will we likely start with when getting data from an API?
PULL()
GIMME()
GET()
SUMMON()
If
GET()
successfully requested information from the API, whatstatus
should you see in the response object?How many people are currently aboard the International Space Station?