Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe. http://developer.nytimes.com/docs
API selected: Most emailed articles and blog posts in the world section. Scope: past 30 days.
library(knitr)
library(httr)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
##
## flatten
library(stringr)
API_key <- "08c14326b23c4d28bb479a29034f0f56"
url<- "http://api.nytimes.com/svc/mostpopular/v2/mostemailed/world/1.json"
NYTmostShared<- GET(url,query = list(api_key = API_key), add_headers(Name="NYT Most Emailed Articles"), verbose())
details<- content(NYTmostShared, "text")
parsed<- jsonlite::fromJSON(details, simplifyVector = TRUE)
mostemailed <- parsed$results
colnames(mostemailed)
## [1] "url" "count_type" "column" "section"
## [5] "byline" "title" "abstract" "published_date"
## [9] "source" "des_facet" "org_facet" "per_facet"
## [13] "geo_facet" "media"
toparticles<-mostemailed %>% dplyr::select(url, byline, title, published_date) %>% rename(authors=byline)
##break down author col into multiple columns
maxcol<- max(sapply(strsplit(as.character(toparticles$authors),'and'),length))
toparticles<- toparticles %>% separate(authors,paste0("author",1:maxcol), sep="and")
## Warning: Too few values at 15 locations: 1, 3, 4, 7, 8, 10, 11, 12, 13, 14,
## 15, 16, 18, 19, 20
toparticles[,2]<- sapply (toparticles[,2], function(x) str_trim(str_replace_all(x, "By", "")))
kable(toparticles,
caption="NYT most emailed articles and blog posts (World Section)")
url | author1 | author2 | title | published_date |
---|---|---|---|---|
http://www.nytimes.com/2017/04/01/world/europe/yevgeny-yevtushenko-dead-dissident-soviet-poet.html | RAYMOND H. ANDERSON | NA | Yevgeny Yevtushenko, Poet Who Stirred a Generation of Soviets, Dies at 83 | 2017-04-01 |
http://www.nytimes.com/2017/03/31/world/americas/penis-seat-mexico-city-harassment.html | SOPAN DEB | MARINA FRANCO | ‘Penis Seat’ Causes Double Takes on Mexico City Subway | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/europe/a-pioneering-german-feminist-looks-back-in-anguish.html | ALISON SMALE | NA | A Pioneering German Feminist Looks Back in Anguish | 2017-03-31 |
http://www.nytimes.com/interactive/2017/03/30/world/africa/the-road-to-nowhere-niger.html | DIONNE SEARCEY | NA | Fleeing Boko Haram, Thousands Cling to a Road to Nowhere | 2017-03-30 |
http://www.nytimes.com/2017/04/01/world/americas/venezuela-populism-authoritarianism.html | MAX FISHER | AMANDA TAUB | How Does Populism Turn Authoritarian? Venezuela Is a Case in Point | 2017-04-01 |
http://www.nytimes.com/2017/03/30/world/americas/venezuelas-supreme-court-takes-power-from-legislature.html | NICHOLAS CASEY | PATRICIA TORRES | Venezuela Muzzles Legislature, Moving Closer to One-Man Rule | 2017-03-30 |
http://www.nytimes.com/2017/04/01/world/europe/brexit-scotland-independence-vote.html | STEVEN ERLANGER | NA | After Brexit, Scotland Lays a Path to Another Independence Vote | 2017-04-01 |
http://www.nytimes.com/2017/03/31/world/asia/myanmar-china-myitsone-dam-project.html | MIKE IVES | NA | A Chinese-Backed Dam Project Leaves Myanmar in a Bind | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/americas/venezuela-political-prisoners-nicolas-maduro.html | NICHOLAS CASEY | ANA VANESSA HERRERO | Ranks of Political Prisoners Grow as Democracy Ebbs in Venezuela | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/americas/un-envoy-nikki-haley-steven-groves-heritage-foundation.html | SOMINI SENGUPTA | NA | U.N. Envoy Draws From Playbook of an Aide Steeped in Conservative Ideology | 2017-03-31 |
http://www.nytimes.com/2016/12/30/world/asia/china-catholic-vatican.html | JAVIER C. HERNÁNDEZ | NA | Catholic Churches in China Should Be Independent of Vatican, Official Says | 2016-12-30 |
http://www.nytimes.com/2017/04/01/world/americas/colombia-flood-mudslide-mocoa.html | RUSSELL GOLDMAN | NA | Mudslide in Colombia: Death Toll Surges to More Than 230 | 2017-04-01 |
http://www.nytimes.com/2017/04/01/world/africa/international-criminal-court-congo-war-crimes.html | STEVE WEMBI | NA | International Court Says Violence in Congo May Amount to War Crimes | 2017-04-01 |
http://www.nytimes.com/2017/03/31/world/europe/uk-brexit-eu-donald-tusk.html | JAMES KANTER | NA | On ‘Brexit,’ Its Divorce First, Trade Talks Later, E.U. Tells U.K. | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/europe/turkey-kurds-crackdown-terrorism.html | PATRICK KINGSLEY | NA | Turkeys Wars: Quelling Kurdish Uprisings, and Suffering From Terror Attacks | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/europe/rex-tillerson-nato.html | GARDINER HARRIS | NA | Tillerson Delivers NATO Allies a Familiar Message: Pay Up | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/asia/india-gujarat-cow-slaughter-penalty.html | NIDA NAJAR | SUHASINI RAJ | Indian State Is Expanding Penalty for Killing a Cow to Life in Prison | 2017-03-31 |
http://www.nytimes.com/2017/03/31/world/americas/venezuela-maduro-national-assembly-attorney-general-ortega.html | NICHOLAS CASEY | NA | As Criticism Mounts, Venezuela Asks High Court to Revisit Power Grab | 2017-03-31 |
http://www.nytimes.com/2017/03/30/world/europe/alaska-russia-sale-150.html | EVAN GERSHKOVICH | NA | 150 Years After Sale of Alaska, Some Russians Have Second Thoughts | 2017-03-30 |
http://www.nytimes.com/2017/03/30/world/asia/maldives-tourism-beach-erosion.html | KAI SCHULTZ | NA | As the Maldives Gains Tourists, Its Losing Its Beaches | 2017-03-30 |