#The jsonlite package is a JSON parser/generator for R which is optimized for pipelines and web APIs. 
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.1.3
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:utils':
## 
##     View
library(curl)
## Warning: package 'curl' was built under R version 3.1.3
#url for the top stories, and the API key is added to that
url <-"http://api.nytimes.com/svc/topstories/v1/home.json?api-key=c7fe74e2f33fe43d639c660b2efcea3b:16:71850816"
req <-fromJSON(url)
topstories <-req$results
class(req)
## [1] "list"
topstories.dataframe <-as.data.frame(req)
colnames(topstories.dataframe)
##  [1] "status"                      "copyright"                  
##  [3] "last_updated"                "num_results"                
##  [5] "results.section"             "results.subsection"         
##  [7] "results.title"               "results.abstract"           
##  [9] "results.url"                 "results.byline"             
## [11] "results.item_type"           "results.updated_date"       
## [13] "results.created_date"        "results.published_date"     
## [15] "results.material_type_facet" "results.kicker"             
## [17] "results.des_facet"           "results.org_facet"          
## [19] "results.per_facet"           "results.geo_facet"          
## [21] "results.multimedia"
# display a subset of the list of topstories
subset(topstories.dataframe,select =c("results.subsection","results.title","results.abstract","results.url"))
##      results.subsection
## 1           Middle East
## 2                Europe
## 3           Middle East
## 4                Europe
## 5                      
## 6  Energy & Environment
## 7              Politics
## 8    Sunday Book Review
## 9                      
## 10                     
## 11                     
## 12                     
## 13                     
## 14               Europe
## 15             DealBook
## 16                Music
##                                                  results.title
## 1           American-Made Weapons Fuel the Wars of Arab States
## 2  Disaster Exposes Industry’s Denial of Risk of Pilot Suicide
## 3  ISIS Video Purports to Show Killing of Ethiopian Christians
## 4           Hundreds Feared Dead After Boat Capsizes Off Libya
## 5      Abuse of Attention Deficit Pills Graduates to Workplace
## 6                       Utilities See Solar Panels as a Threat
## 7    At G.O.P. Event, All Talk Is of Clinton (None of It Good)
## 8                         Toni Morrison’s ‘God Help the Child’
## 9                   Reclaiming the Age-Old Art of Getting Lost
## 10                              A Generous (and Unwanted) Gift
## 11                            Surveying How Their Gardens Grow
## 12              Police Unions Rethink How to Address Shootings
## 13            Indiana Says Outbreak of H.I.V. Is Getting Worse
## 14         Vivian Nicholson, Rags-to-Riches-to-Rags Icon, Dies
## 15                  DealBook: The Power of Starting With ‘Yes’
## 16             Generational Push and Pull at Rock Hall of Fame
##                                                                                                                                                                                                                                                     results.abstract
## 1  The sectarian conflicts and battles against terrorist networks in the Middle East have created a boom for American defense contractors looking for foreign business — but also raise the prospect of a dangerous new arms race in a region of shifting alliances.
## 2                                                                After Andreas Lubitz flew himself and 149 others into a French mountainside last month, investigations have exposed weaknesses by the airline industry in dealing with mental illness among pilots.
## 3                                                       The footage, if confirmed, would be the first evidence that the Islamic State’s leaders in Syria and Iraq are coordinating with fighters who have taken up the group’s banner in southern and eastern Libya.
## 4                                                                                                                                         The boat carrying hundreds of migrants was trying to reach Italy when it capsized in the Mediterranean Sea north of Libya.
## 5                                                                                                                                                 Interviews suggest more young American workers are taking stimulants to enhance concentration and stamina at work.
## 6                                                                                   Hawaii is at the forefront of a global upheaval in the power business, as homemade electricity puts new pressure on old infrastructure and the revenue of electricity companies.
## 7                                                                                                                               During a gathering in New Hampshire, an energized, confident bench of 19 Republicans took turns taking apart Hillary Rodham Clinton.
## 8                                                                                                                           Child abuse cuts a jagged scar through Toni Morrison’s “God Help the Child,” a modern-­day fairy tale with shades of the Brothers Grimm.
## 9                                                                                                                                                                                           Ditching modern-day navigation apps in favor of wandering and discovery.
## 10                                                                                                                                                                                   This offer was different, and not just because a house is obviously a big gift.
## 11                              Deborah Marton, executive director of the New York Restoration Project, which tends to two parks and 52 community gardens, spends her time off with her son and her dog, while also checking up on other groups&#8217; green spaces.
## 12                                   Though they have historically defended officers suspected of wrongdoing and denounced what they see as efforts to undermine the police, unions are reconsidering how best to deliver their message in the face of public anger.
## 13                                                                                                                                Indiana health officials said Friday there are 120 confirmed cases tied to a county that recently began a needle exchange program.
## 14                                                                                                                    Mrs. Nicholson was just 25 when she won a sports bet that paid more than £150,000, and her life inspired a musical and a BBC film. She was 79.
## 15                                                                                          While our first instinct may be “no,” negativity shuts down collaboration and innovation. “Yes,” on the other hand, opens up trust and creativity, Tony Schwartz writes.
## 16                                                                                                       Even as a millennial ambassador like Miley Cyrus shared the stage with Joan Jett, the ceremony was about holding up consensus icons like the late Lou Reed.
##                                                                                                                            results.url
## 1                                http://www.nytimes.com/2015/04/19/world/middleeast/sale-of-us-arms-fuels-the-wars-of-arab-states.html
## 2                   http://www.nytimes.com/2015/04/19/world/europe/germanwings-plane-crash-andreas-lubitz-lufthansa-pilot-suicide.html
## 3                  http://www.nytimes.com/2015/04/20/world/middleeast/isis-video-purports-to-show-killing-of-ethiopian-christians.html
## 4                                           http://www.nytimes.com/2015/04/20/world/europe/italy-migrants-capsized-boat-off-libya.html
## 5                              http://www.nytimes.com/2015/04/19/us/workers-seeking-productivity-in-a-pill-are-abusing-adhd-drugs.html
## 6  http://www.nytimes.com/2015/04/19/business/energy-environment/solar-power-battle-puts-hawaii-at-forefront-of-worldwide-changes.html
## 7                         http://www.nytimes.com/2015/04/19/us/gop-hopefuls-in-new-hampshire-attack-clinton-more-than-one-another.html
## 8                                                http://www.nytimes.com/2015/04/19/books/review/toni-morrisons-god-help-the-child.html
## 9                                                                      http://www.nytimes.com/2015/04/19/travel/19EssayLostEurope.html
## 10                                                           http://www.nytimes.com/2015/04/19/style/a-generous-and-unwanted-gift.html
## 11                                                    http://www.nytimes.com/2015/04/19/nyregion/surveying-how-their-gardens-grow.html
## 12                        http://www.nytimes.com/2015/04/19/us/police-unions-facing-public-anger-rethink-how-to-address-shootings.html
## 13                                                    http://www.nytimes.com/2015/04/19/us/indiana-says-hiv-outbreak-is-worsening.html
## 14                         http://www.nytimes.com/2015/04/19/world/europe/vivian-nicholson-rags-to-riches-to-rags-icon-dies-at-79.html
## 15                                             http://www.nytimes.com/2015/04/18/business/dealbook/the-power-of-starting-with-yes.html
## 16  http://www.nytimes.com/2015/04/20/arts/music/rock-and-roll-hall-of-fame-ceremony-joan-jett-bill-withers-green-day-ringo-starr.html