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.
For the first example I will use the MOST POPULAR API, to load the most shared articles by facebook.
url <- paste("https://api.nytimes.com/svc/mostpopular/v2/shared/1/facebook.json?api-key=8Tuw2G2y7TwdrHs4sejvtQWjJ2E844o5")
rawDataFromMostPopularApi <- fromJSON(url,flatten = T)
resultsDataFramemostpopular <- rawDataFromMostPopularApi$results
resultsDataFramemostpopular %>%
select(title, section, source, abstract, type, byline, adx_keywords, published_date,updated) %>%
datatable()
I will now make a bar graph showing the number of popular articles per section.
resultsDataFramemostpopular %>% ggplot(aes(x=section, fill = section)) + geom_bar()
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()
I will now make a bar graph showing the number of articles per section.
resultsDataFrameTopStories %>% ggplot(aes(x=section, fill = section)) + geom_bar()
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()
I will now make a bar graph showing the number of articles per section.
resultsDataFrameTopStoriesTechnology %>% ggplot(aes(x=section, fill = section)) + geom_bar()
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.