Introduction

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.

Using the Top Stories API per Science

For the second 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=8Tuw2G2y7TwdrHs4sejvtQWjJ2E844o5")

rawDataFromTopStoriesApi <- fromJSON(url,flatten = T)
resultsDataFrameTopStories <- rawDataFromTopStoriesApi$results

resultsDataFrameTopStories %>% 
  select( title,section, short_url, abstract) %>% 
  datatable()

Display Results Graphically for Top Stories API per science

I will now make a bar graph showing the number of articles per section.

resultsDataFrameTopStories %>% ggplot(aes(x=section, fill = section)) + geom_bar()

Using the Top Stories API per Technology

For the third example I will use the Top Stories API, to load the top technology stories.

url <- paste("https://api.nytimes.com/svc/topstories/v2/technology.json?api-key=8Tuw2G2y7TwdrHs4sejvtQWjJ2E844o5")

rawDataFromTopStoriesApiTechnology <- fromJSON(url,flatten = T)
resultsDataFrameTopStoriesTechnology <- rawDataFromTopStoriesApiTechnology$results

resultsDataFrameTopStories %>% 
  select( title,section, short_url, abstract) %>% 
  datatable()

Display Results Graphically for Top Stories API per technology

I will now make a bar graph showing the number of articles per section.

resultsDataFrameTopStoriesTechnology %>% ggplot(aes(x=section, fill = section)) + geom_bar()

Conclusion

As can be seen, it is quite useful and simple to access data using an API that returns JSON, I used three different API’s and the results of these API are display above.