In this assignment we have been asked to use the New York Times Developer API to obtain information and pull it into a R data frame.
I will be storing the API Key in the local keychain of my computer, I will use the keyring package to access the key.
api.key<- keyring::key_get("ny_times_api_key_data607")
For the first example I will use the Top Stories API, to load the top science stories.
url <- paste("https://api.nytimes.com/svc/topstories/v2/science.json?api-key=", api.key)
rawDataFromTopStoriesApi <- fromJSON(url,flatten = T)
resultsDataFrameTopStories <- rawDataFromTopStoriesApi$results
resultsDataFrameTopStories %>%
select( title,section, short_url, abstract) %>%
datatable()
I will now make a bar graph showing the number of articles per section.
resultsDataFrameTopStories %>% ggplot(aes(x=section)) +geom_bar()
As can be seen, it is quite useful and simple to access data using an API that returns JSON.