Intro

The purpose of this lab is to create an account with the NYT NPI’s website and controct an R dataframe using the JSON file that is pulled in.

Loading Libraries

library("jsonlite")
library("httr")
library("stringr")
library("ggplot2")
library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Loading in API data

stories <- GET('https://api.nytimes.com/svc/topstories/v2/science.json?api-key=n5H7ZDIyqy8LAcTpAu9mIdUi7oTA6WOx')

summary(stories)
##             Length Class       Mode       
## url             1  -none-      character  
## status_code     1  -none-      numeric    
## headers        23  insensitive list       
## all_headers     1  -none-      list       
## cookies         7  data.frame  list       
## content     55657  -none-      raw        
## date            1  POSIXct     numeric    
## times           6  -none-      numeric    
## request         7  request     list       
## handle          1  curl_handle externalptr

Making a Dataframe

stories_text <- content(stories, as = "text")
## No encoding supplied: defaulting to UTF-8.
stories_fromJson <- fromJSON(stories_text, flatten = TRUE)
stories_df <- data.frame(stories_fromJson$results, stringsAsFactors = FALSE)
head(stories_df)
##   section subsection
## 1 science           
## 2   admin           
## 3 climate           
## 4  health           
## 5 science           
## 6  health           
##                                                                    title
## 1                               Sign Up for the Science Times Newsletter
## 2                                                                       
## 3                                            Can 70 Moms Save a Species?
## 4            A Trans Researcher’s Pursuit of Better Data on Detransition
## 5 Leon Cooper Dies at 94; Nobelist Unlocked Secrets of Superconductivity
## 6   E. Coli Outbreak Tied to McDonald’s Widens to 75 People in 13 States
##                                                                                                                                                                                             abstract
## 1                                                                                             Every week, we’ll bring you stories that capture the wonders of the human body, nature and the cosmos.
## 2                                                                                                                                                                                                   
## 3                 Here’s the story of Squilla, a rare North Atlantic right whale mother, and her firstborn. To help their species continue, they’d have to navigate an increasingly dangerous ocean.
## 4                                            Kinnon MacKinnon leads the world’s largest study on people who stop or reverse their gender transitions, a group embroiled in intense political fights.
## 5                                      He shared the 1972 physics prize for showing how some materials could convey electricity without resistance. He also did pioneering research in neuroscience.
## 6 Health officials say that recalls of onions — the suspected source of the contamination — would help lessen the risk to consumers. Other major fast-food chains have also stopped offering onions.
##                                                                                                 url
## 1                                                                                              null
## 2                                                                                                  
## 3 https://www.nytimes.com/interactive/2024/10/26/climate/north-atlantic-right-whale-population.html
## 4             https://www.nytimes.com/2024/10/26/health/kinnon-mackinnon-detransition-research.html
## 5                                  https://www.nytimes.com/2024/10/25/science/leon-cooper-dead.html
## 6            https://www.nytimes.com/2024/10/25/health/e-coli-mcdonalds-quarter-pounder-onions.html
##                                                              uri
## 1 nyt://embeddedinteractive/daba9d03-29b8-5cfc-8cbd-74b8abf54a07
## 2 nyt://embeddedinteractive/6cb185ed-65ac-50a5-ace3-5bab81934245
## 3         nyt://interactive/b7174af5-7083-52fc-8f94-14dfeae60af3
## 4             nyt://article/b600de0f-119b-5bcc-808f-7fe7a97e8f64
## 5             nyt://article/5e117a07-d6a2-5d7b-bed5-0fd9c300a54a
## 6             nyt://article/886c08bb-d002-53a2-9547-23de5323cd5b
##                  byline           item_type              updated_date
## 1                       EmbeddedInteractive 2018-04-07T13:23:25-04:00
## 2                       EmbeddedInteractive 2015-07-20T13:45:03-04:00
## 3     By Catrin Einhorn         Interactive 2024-10-27T11:47:20-04:00
## 4    By Azeen Ghorayshi             Article 2024-10-26T13:22:03-04:00
## 5 By Dylan Loeb McClain             Article 2024-10-26T21:06:24-04:00
## 6   By Teddy Rosenbluth             Article 2024-10-25T22:36:32-04:00
##                created_date            published_date material_type_facet
## 1 2016-02-05T18:18:53-05:00 2016-02-05T18:18:53-05:00                    
## 2 2015-04-16T14:13:18-04:00 2015-04-16T14:13:18-04:00                    
## 3 2024-10-26T05:05:41-04:00 2024-10-26T05:05:41-04:00                    
## 4 2024-10-26T05:02:20-04:00 2024-10-26T05:02:20-04:00                    
## 5 2024-10-25T16:25:28-04:00 2024-10-25T16:25:28-04:00                    
## 6 2024-10-25T13:40:56-04:00 2024-10-25T13:40:56-04:00                    
##                kicker
## 1                    
## 2                    
## 3                    
## 4 A Conversation With
## 5                    
## 6                    
##                                                                                                                                                                                                                                                                              des_facet
## 1                                                                                                                                                                                                                                                                                     
## 2                                                                                                                                                                                                                                                                                     
## 3 Whales and Whaling, Endangered and Extinct Species, Fishing, Commercial, Lobsters, Crabs, Fish and Other Marine Life, Oceans and Seas, Conservation of Resources, Biodiversity, Global Warming, Coastal Areas, Wind Power, Animal Migration, Boats and Boating, Accidents and Safety
## 4                                                                                                                                                                 Transgender, Research, Gender, Youth, Hormones, Teenagers and Adolescence, Media, Testosterone, your-feed-healthcare
## 5                                                                                                                                                                                                                Deaths (Obituaries), Superconductors, Physics, Research, Nobel Prizes
## 6                                                                                      Food Contamination and Poisoning, Fast Food Industry, E Coli (Bacteria), Recalls and Bans of Products, Regulation and Deregulation of Industry, Meat, Onions, Hamburgers, Content Type: Service
##                                                                                                                                                             org_facet
## 1                                                                                                                                                                    
## 2                                                                                                                                                                    
## 3                                                                                            Maine Lobstermen's Assn, National Oceanic and Atmospheric Administration
## 4                                                                                                                                                                    
## 5                                                                                                          Brown University, University of Illinois, Urbana-Champaign
## 6 Burger King Corp, Centers for Disease Control and Prevention, Food and Drug Administration, McDonald's Corporation, Taylor Farms, Kentucky Fried Chicken, Pizza Hut
##                                          per_facet
## 1                                                 
## 2                                                 
## 3 Moore, Michael (1954- ), Mills, Janet T (1947- )
## 4                                                 
## 5                                   Cooper, Leon N
## 6                                                 
##                                                                                                    geo_facet
## 1                                                                                                           
## 2                                                                                                           
## 3 Bay of Fundy (Canada), Maine, Gulf of St Lawrence, Canada, East Coast (US), Cape Cod (Mass), United States
## 4                                                                                                           
## 5                                                                                                           
## 6                                                                                                   Colorado
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          multimedia
## 1                                                                                                                                                                                                                                                                                                                                                                                                                         https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-superJumbo.jpg, https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-mediumThreeByTwo440.jpg, https://static01.nyt.com/images/2016/02/06/science/sciencenewsletter2/sciencenewsletter2-thumbLarge.jpg, Super Jumbo, mediumThreeByTwo440, Large Thumbnail, 1534, 293, 150, 2048, 440, 150, image, image, image, photo, photo, photo, , , , NASA, via Associated Press, NASA, via Associated Press, NASA, via Associated Press
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              NULL
## 3                                                                                                                                                                                                                                                                    https://static01.nyt.com/images/2024/10/25/climate/00cli-whales-static-promo/00cli-whales-static-promo-superJumbo.jpg, https://static01.nyt.com/images/2024/10/25/climate/00cli-whales-static-promo/00cli-whales-static-promo-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2024/10/25/climate/00cli-whales-static-promo/00cli-whales-static-promo-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 800, 400, 150, 1200, 600, 150, image, image, image, photo, photo, photo, , , , Georgia Department of Natural Resources, NOAA permit #20556-01, Georgia Department of Natural Resources, NOAA permit #20556-01, Georgia Department of Natural Resources, NOAA permit #20556-01
## 4                                                                                                                                                                                                                                                                                  https://static01.nyt.com/images/2024/10/22/multimedia/SCI-CONVERSATION-MACKINNON-03-tkqz/SCI-CONVERSATION-MACKINNON-03-tkqz-superJumbo.jpg, https://static01.nyt.com/images/2024/10/22/multimedia/SCI-CONVERSATION-MACKINNON-03-tkqz/SCI-CONVERSATION-MACKINNON-03-tkqz-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2024/10/22/multimedia/SCI-CONVERSATION-MACKINNON-03-tkqz/SCI-CONVERSATION-MACKINNON-03-tkqz-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1391, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, , , , Laura Proctor for The New York Times, Laura Proctor for The New York Times, Laura Proctor for The New York Times
## 5                      https://static01.nyt.com/images/2024/10/27/multimedia/25cooper1-fmch-print1/25cooper1-fmch-superJumbo.jpg, https://static01.nyt.com/images/2024/10/27/multimedia/25cooper1-fmch-print1/25cooper1-fmch-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2024/10/27/multimedia/25cooper1-fmch-print1/25cooper1-fmch-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1579, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, The physicist Leon N. Cooper in 1972, after it was announced that he and two colleagues had been awarded the Nobel Prize in Physics., The physicist Leon N. Cooper in 1972, after it was announced that he and two colleagues had been awarded the Nobel Prize in Physics., The physicist Leon N. Cooper in 1972, after it was announced that he and two colleagues had been awarded the Nobel Prize in Physics., Bettmann/Getty Images, Bettmann/Getty Images, Bettmann/Getty Images
## 6 https://static01.nyt.com/images/2024/10/25/multimedia/25ECOLI-MCDONALDS-blkh/25ECOLI-MCDONALDS-blkh-superJumbo.jpg, https://static01.nyt.com/images/2024/10/25/multimedia/25ECOLI-MCDONALDS-blkh/25ECOLI-MCDONALDS-blkh-threeByTwoSmallAt2X.jpg, https://static01.nyt.com/images/2024/10/25/multimedia/25ECOLI-MCDONALDS-blkh/25ECOLI-MCDONALDS-blkh-thumbLarge.jpg, Super Jumbo, threeByTwoSmallAt2X, Large Thumbnail, 1366, 400, 150, 2048, 600, 150, image, image, image, photo, photo, photo, The outbreak, which officials believe might be traced to the burger’s slivered onions, had sickened 75 people across 13 states., The outbreak, which officials believe might be traced to the burger’s slivered onions, had sickened 75 people across 13 states., The outbreak, which officials believe might be traced to the burger’s slivered onions, had sickened 75 people across 13 states., Brendan Mcdermid/Reuters, Brendan Mcdermid/Reuters, Brendan Mcdermid/Reuters
##   short_url
## 1          
## 2          
## 3          
## 4          
## 5          
## 6